0

I am using Sql server 2014 and made a ADO.NET Entity Data Model. It generated this model: enter image description here

I am trying to add mails I retrieve into my database. It looks like this:

    public int StoreMail(PhishingMail PhishingMail)
    {
        using (var phishingMailStorage = new PhishFinderDB2Entities2())
        {
          //  phishingMailStorage.Database.Connection.Open();

            phishingMailStorage.PhishingMail.Add(PhishingMail);
            phishingMailStorage.SaveChanges();
        }
        return PhishingMail.PhishingMailId;
    }

Whenever I debug the program. I am stuck at this part:

phishingMailStorage.PhishingMail.Add(PhishingMail);

It gives me the metadataexception was unhandeld exception:An unhandled exception of type 'System.Data.Entity.Core.MetadataException' occurred in EntityFramework.dll Additional information: Unable to load the specified metadata resource.

This is my connection string: I really don't know why it generated this gigantic string. It may cause the problem.

<add name="PhishFinderDB2Entities2" connectionString="metadata=res://*/DataAccess.PhishFinderModel.csdl|res://*/DataAccess.PhishFinderModel.ssdl|res://*/DataAccess.PhishFinderModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=WIN7DEV;initial catalog=PhishFinderDB;integrated security=True;pooling=False;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /><add name="PhishFinderDB2Entities" connectionString="metadata=res://*/DataAccess.PhishFinderModel.csdl|res://*/DataAccess.PhishFinderModel.ssdl|res://*/DataAccess.PhishFinderModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=PhishFinderDB2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /><add name="PhishFinderDB2" connectionString="metadata=res://*/DataAccess.Model1.csdl|res://*/DataAccess.Model1.ssdl|res://*/DataAccess.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=WPNLL0038874\SQLEXPRESS;initial catalog=PhishFinderDB2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

Do you know why I get this exception? Thanks

user3599415
  • 283
  • 3
  • 6
  • 18
  • can this be that your PhishingMail class is static and the parameter PhishingMail is confused with your class name? Maybe it's not the reason - but you should keep your parameters lower case in any case in line with convention e.g. public int StoreMail(PhishingMail phishingMail) – Turo Jan 25 '16 at 14:30

1 Answers1

1

Try to delete the generated PhishFinderDB2.edmx (keep in mind to save data, if you made your own adjustments). Delete also the connectionstring. Then regenerate the whole PhishFinderDB2 ado.net model. For me this helped multiple times.

M. Schena
  • 2,039
  • 1
  • 21
  • 29
  • that helped a bit But now i get this error at the savechanges method: An unhandled exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. – user3599415 Jan 25 '16 at 14:50
  • In 'public int StoreMail(PhishingMail PhishingMail)' you use the Variable in the syntax of an 'PhinshingMail-Object', you wrote it both exact the same way. Maybe you should rename to 'public int StoreMail(PhishingMail phishingMail)'. I hope this helps. – M. Schena Jan 25 '16 at 14:53
  • thanks, I changed that. I still get the same exception. But aleast I am one step further. – user3599415 Jan 25 '16 at 14:55
  • Hmm, what i would do is comparing your values of the object with the DB-Values, if all constraint are set properly in code. – M. Schena Jan 25 '16 at 15:05
  • I just found out, that you can debug all these Exceptions. Have a look at http://stackoverflow.com/questions/5400530/validation-failed-for-one-or-more-entities-while-saving-changes-to-sql-server-da or: http://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert – M. Schena Jan 25 '16 at 15:07