2017-05-10 4 views
0

Ich möchte Datensätze mit Fremdschlüsseln hinzufügen, um nicht die Nummer ID zu schreiben.Datensatz hinzufügen in Methode Seed mit Fremdschlüssel Entity Framework

protected override void Seed(ClockShopEntities context) 
    { ... } // Seed 

    private void AddType(ClockShopEntities context) 
    { 
     context.Typs.Add(new Typ { typName = "name1" }); 
     context.Typs.Add(new Typ { typName = "name2" }); 
    } // AddType 

    private void AddCountry(ClockShopEntities context) 
    { 
     context.Countries.Add(new Country { countryName = "country1" }); 
     context.Countries.Add(new Country { countryName = "country2" }); 
     context.Countries.Add(new Country { countryName = "country3" }); 
     context.Countries.Add(new Country { countryName = "country4" }); 
    } // AddCountry 

Wie kann ich einen Wert hinzufügen, indem Sie einen Namen angeben?

// !!!!! with write id 
    private void AddFabricator(ClockShopEntities context) 
    { 
     context.Fabricators.Add(new Fabricator { fName = "", idCountry = 1 }); 
    } // AddFabricator 


    // How can I write without Id 
    private void AddFabricator(ClockShopEntities context) 
    { 
     context.Fabricators.Add(
      new Fabricator { fName = "", idCountry = ?? where Country = "country1" }); 
    } // AddFabricator 

ich Hilfe hoffen, Emma

Antwort

1

Sie können nicht von Id zu diesem Zeitpunkt als Country hinzufügen noch nicht in der Datenbank eingefügt. Was Sie stattdessen tun müssen, ist die Country Eigenschaft Fabricator auf die neue Country wird eingefügt und Entity Framework einstellen würde sich die CountryId der Einstellung auf Fabricator kümmern, wenn Fabricator in der Datenbank eingefügt wird.

sollten Sie diesen Code unter eine Vorstellung geben:

private void AddCountry(ClockShopEntities context) 
{ 
    var country1 = new Country { countryName = "country1" }; 
    AddFabricator(context, country1); 
    context.Countries.Add(country1); 

    context.Countries.Add(new Country { countryName = "country2" }); 
    context.Countries.Add(new Country { countryName = "country3" }); 
    context.Countries.Add(new Country { countryName = "country4" }); 
} // AddCountry 

private void AddFabricator(ClockShopEntities context, Country country) 
{ 
    context.Fabricators.Add(new Fabricator { fName = "", Country = country }); 
} // AddFabricator 

Alternativ können Sie auch die lokale Land erhalten, indem Namen Country Eigenschaft Fabricator (das ist noch nicht hinzugefügt) und weisen Sie das.

private void AddFabricator(ClockShopEntities context) 
{ 
    context.Fabricators.Add(new Fabricator { fName = "", Country = context.Countries.First(c => c.countryName == "country1") }); 
} // AddFabricator 
+0

Der Typ 'string' kann nicht implizit in 'bool' konvertiert werden. Der Lambda-Ausdruck kann nicht in den Delegattyp konvertiert werden, da einige der Rückgabetypen im Block nicht implizit in den Rückgabetyp des Delegierten konvertierbar sind. –

+0

überprüfen Sie den aktualisierten Code, ich hatte ein Gleichheitszeichen in der Bedingung verpasst. – sachin

+0

ja! Danke vielmals –

Verwandte Themen