0
我試圖做到這一點關係的衝突關係和外鍵1-1
public class Company {
public int Id { get; set; }
public Configuration Configuration { get; set; }
}
public class Configuration {
public int Id { get; set; }
// public int CompanyId { get; set; } -> can't do this
public Company Company { get; set; }
}
public class ConfigurationMapping : EntityTypeConfiguration<Configuration> {
public ConfigurationMapping {
HasRequired(configuration => configuration.Company)
.WithOptional(company => company.Configuration)
// .HasForeignKey(configuration => configuration.CompanyId) -> this doesn't exist
.Map(f => f.MapKey("CompanyId")); // that's why I can't use the property above
}
}
我不明白我怎麼可以添加一個Configuration
並設置IdCompany
。 還有另一種方法嗎? 我該怎麼做?
db.Configurations.Add(new Configuration {
IdCompany = idCompany
});
db.SaveChanges();
瞭解,但是如何才能爲公司設置值?我必須檢索公司並設置配置。公司=公司? – 2011-12-30 14:16:05
我加了一些例子。它適用於一對多的場景,沒有任何問題,所以我希望它也可以一對一地工作。 – 2011-12-30 14:34:47