Good day.

There is a database of two tables. The structure of the tables is built through the Entity Framework Code First. The structure is as follows:

public class FederationMembers { public Guid ID { get; set; } [Required MaxLength(10)] public string ISO_Code { get; set; } [Required MaxLength(100)] public string Title { get; set; } [Required MaxLength(3)] public string Number { get; set; } } public class LawEnforcement { public Guid ID { get; set; } [Required MaxLength(200)] public string Title { get; set; } [MaxLength(300)] public string Address { get; set; } public FederationMembers SubjectOfFederation { get; set; } } 

The FederationMembers table is pre-populated at the stage of database initialization. I am trying to make an entry in the LawEnforcement table, and I get an error that the system is trying to add PrimeryKey to the FederationMembers table that already exists. In the SQL profiler is the code

 INSERT [tbl].[FederationMembers]([ID], [ISO_Code], [Title], [NUMBER]) VALUES (@0, @1, @2, @3) 

The code for adding an entry to the LawEnforcement table below

 Context context = new Context(); Models.LawEnforcement lea = new Models.LawEnforcement(); lea.ID = Guid.NewGuid(); lea.Title = Request["lea_title"]; lea.Address = Request["address"]; lea.SubjectOfFederation = repo.FederationMembers.Where(c => c.ID.ToString() == Request["lea_fm"]).First(); context.LawEnforcement.Add(lea); context.SaveChanges(); 

Why does the system try to add an entry to FederationMembers when I try to add an entry to the LawEnforcement.

    1 answer 1

    Try the following:

    1. Add another property to the LawEnforcement class:

       public Guid SubjectOfFederationID { get; set; } 
    2. Make the SubjectOfFederation property virtual:

       public virtual FederationMembers SubjectOfFederation { get; set; } 
    3. Add as follows:

       using(Context context = new Context()) { var federationMember = context.FederationMembers.Where(c => c.ID.ToString() == Request["lea_fm"]).First(); if (federationMember != null) { var lea = new Models.LawEnforcement() { ID = Guid.NewGuid(), Title = Request["lea_title"], Address = Request["address"], SubjectOfFederationID = federationMember.ID }; context.LawEnforcement.Add(lea); context.SaveChanges(); } } 

    After adding and changing class properties, you need to apply the changes to the database.

    • Thank you so much really helped. But could you tell me how the “lazy” loading affects the addition of elements to the table? - hamec
    • @hamec, lazy loading does not affect the addition of elements to the table, but is merely syntactic sugar. Helps load data on demand until context is released through Dispose. Affects adding a field with a specific type. For EF, it is important that there is a field with a type and the same field with the same name, but ending in ID. You can find a lot of information about lazy loading, and as for adding data, just read about the EF rules and you will understand everything. - Denis Bubnov