简体   繁体   中英

EF: How to reuse newly created foreign key

I am new to EF and stuck here in one of my scenerio, where I need to create or use keys from a reference table. User can create a new record in reference table or can use existing one. For that I have created my Entity model as shown below:

 Public class MaterialLineEntry()
 {
    EntryNumber = 0,
    OrganisationID=0,
    Organisation  Organisation 
  };

AND

 Public class Organisation ()
  {
     ID = 0,                            
     Description = "Test",
     EntityChangeStatus ;
   };

These line entries are defined inside another entity called Report

Public class Report()
{
         List<  MaterialLineEntry> materilaLineEntries;
 };

Everything works fine if I enter the details as shown below and attach the object(report), to the UnitOfWork and commet it. Entity framework creates a record in reference table and use the newly generated foreign key to enter it in line entry table.

Report report =new Reort();
        report. materilaLineEntries.Add(new MaterialLineEntry()
            {
                EntryNumber=1,
                OrganisationID=0,
                Organisation=new Organisation()
                {
                    ID = 0,
                    Description = "Test",
                    EntityChangeStatus = EntityChangeStatus.Unchanged,
                }
            })

but when I add two objects to the line entry with same organisation details:

 report. materilaLineEntries.Add(new MaterialLineEntry()
            {
                EntryNumber=1,
                OrganisationID=0,
                Organisation=new Organisation()
                {
                    ID = 0,
                    Description = "Test",
                    EntityChangeStatus = EntityChangeStatus.Unchanged,
                }
            })

it should use the previously created foreign key(as I may have a unique constrant). As I am trying to insert duplicate record in a reference table. What I need here is, Entity Frame work should add the first organization details and should use the same ID for consequent occurences of same record (in this case Description column).

Please help me geeting this solved. Thanks in advance. (I have thought of adding the data in foreign table one by one and using the foreign key further, but I guess it is a bad idea).

You have foreignkey "OrganizationId" in the MaterialLineEntry. To resuse a existing Organization you have to assign an exisitng id to OrganizationId only. eg

report. materilaLineEntries.Add(new MaterialLineEntry()
            {
                EntryNumber=1,
                OrganisationID=0,//Id of a exsiting organization

            });

EDIT : Full code

Report report =new Reort();
var org=new Organisation()
                {
                    Description = "Test",
                    EntityChangeStatus = EntityChangeStatus.Unchanged,
                };
        report. materilaLineEntries.Add(new MaterialLineEntry()
            {
                EntryNumber=1,
                OrganisationID=0,
                Organisation=org
            });
        report.SaveChanges();
        report. materilaLineEntries.Add(new MaterialLineEntry()
                {
                    EntryNumber=1,
                    OrganisationID=org.Id,//Id of a exsiting organization

                });
       report.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM