问题描述
说我有三张桌子。设置,设施和设施在现有数据库中设置。样本数据: 设置表
ID名称值
1 test1 true
2 test2 true
设施表
ID名称
163 Demo1
164 Demo2
FacilitySettingOverride
FacilityId SettingId Value
163 2 False
164 1 False
164 2 False
FacilitySettingOverride有两个外键--- FacilityId和SettingId。另外我想为FacilitySettingOverride创建一个复合键。
我只是不知道如何使它。我的主要代码
[Table(Settings)]
public class设置
{
[Key]
public int Id {get;组; }
public string Name {get;组; }
public string Value {get;组; }
public FacilitySettingOverride FacilitySettingOverride {get;组;
}
[表(设施)]
public class Facility
{
[Key]
public int Id {get ;组; }
public string Name {get;组; }
public virtual ICollection< FacilitySettingOverride> FacilitySettingOverrides {get;组; }
}
[Table(FacilitySettingOverride)]
public class FacilitySettingOverride
{
[Key]
[ForeignKey(FacilityId )]
public int FacilityId {get;组; }
public string Value {get;组; }
[ForeignKey(SettingId)]
public int SettingId {get;组; }
public virtual Facility Facility {get;组; }
public virtual设置设置{get;组; }
我猜这是错误的。感谢您的帮助。
查看您的数据和描述,设置
和工具
具有一对多关联到 FacilitySettingOverride
。所以设置
不应该有一个属性...
public FacilitySettingOverride FacilitySettingOverride {get;组; }
...但是集合 FacilitySettingOverride
这应该是正确的类模型:
[表(设置)]
public class设置
{
[Key]
public int Id {get;组; }
public string Name {get;组; }
public string Value {get;组; }
public virtual ICollection< FacilitySettingOverride> FacilitySettingOverrides {get;组;
}
[表(设施)]
public class Facility
{
[Key]
public int Id {get ;组; }
public string Name {get;组; }
public virtual ICollection< FacilitySettingOverride> FacilitySettingOverrides {get;组; }
}
[Table(FacilitySettingOverride)]
public class FacilitySettingOverride
{
[Key,Column(Order = 1)]
[ForeignKey(Facility)]
public int FacilityId {get;组;
[Key,Column(Order = 2)]
[ForeignKey(Setting)]
public int SettingId {get;组; }
public virtual Facility Facility {get;组; }
public virtual设置设置{get;组; }
public string Value {get;组;
}
KeyAttribute
和 ColumnAttribute
( [Key,Column(Order = 1)]
)用于复合主键。 / p>
这是EF创建的数据库模式:
Say I have three tables. Settings, Facilities and FacilitySettingOverride in an existing database. The sample data:
Settings table
ID Name Value
1 test1 true
2 test2 true
Facilities table
ID Name
163 Demo1
164 Demo2
FacilitySettingOverride
FacilityId SettingId Value
163 2 False
164 1 False
164 2 False
FacilitySettingOverride has two foreign keys---FacilityId and SettingId. Also I want to make a composite key for FacilitySettingOverride.
I just don't know how to make it. My primary code.
[Table("Settings")]
public class Setting
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public FacilitySettingOverride FacilitySettingOverride { get; set; }
}
[Table("Facilities")]
public class Facility
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<FacilitySettingOverride> FacilitySettingOverrides { get; set; }
}
[Table("FacilitySettingOverride")]
public class FacilitySettingOverride
{
[Key]
[ForeignKey("FacilityId")]
public int FacilityId { get; set; }
public string Value { get; set; }
[ForeignKey("SettingId")]
public int SettingId { get; set; }
public virtual Facility Facility { get; set; }
public virtual Setting Setting { get; set; }
I guess that it is wrong. Thanks for help.
Looking at your data and description, both Settings
and Facility
have one-to-many associations to FacilitySettingOverride
. So Setting
shouldn't have a property ...
public FacilitySettingOverride FacilitySettingOverride { get; set; }
... but a collection of FacilitySettingOverride
s, just as Facility
has.
This should be the proper class model:
[Table("Settings")]
public class Setting
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public virtual ICollection<FacilitySettingOverride> FacilitySettingOverrides { get; set; }
}
[Table("Facilities")]
public class Facility
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<FacilitySettingOverride> FacilitySettingOverrides { get; set; }
}
[Table("FacilitySettingOverride")]
public class FacilitySettingOverride
{
[Key, Column(Order = 1)]
[ForeignKey("Facility")]
public int FacilityId { get; set; }
[Key, Column(Order = 2)]
[ForeignKey("Setting")]
public int SettingId { get; set; }
public virtual Facility Facility { get; set; }
public virtual Setting Setting { get; set; }
public string Value { get; set; }
}
The combination of KeyAttribute
and ColumnAttribute
([Key, Column(Order = 1)]
) is used for composite primary keys.
This is the database schema EF creates from it:
这篇关于首先在EF 7代码中创建POCO类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!