C# 多个表的一对多关系
我对EF的数据库设计非常陌生,并试图了解如何正确地将实体模型转换为DB表 我有以下两门课:C# 多个表的一对多关系,c#,.net,database,entity-framework,one-to-many,C#,.net,Database,Entity Framework,One To Many,我对EF的数据库设计非常陌生,并试图了解如何正确地将实体模型转换为DB表 我有以下两门课: public class Test { public int ID { get; set; } // ... more data ICollection<Exception> Exceptions { get;set; } } public class Measurement { public int ID { get; set; } // ...
public class Test
{
public int ID { get; set; }
// ... more data
ICollection<Exception> Exceptions { get;set; }
}
public class Measurement
{
public int ID { get; set; }
// ... more data
ICollection<Exception> Exceptions { get;set; }
}
这样,我的每个异常实体都将有测试表和度量表的外键,但是在每个异常实体中都会显示测试或度量,因为一个异常不能同时来自测试或度量
这是可行的,但我想知道这是正确的做法还是有更好的做法
感谢虚拟EF识别连接 通过虚拟Icollection为许多用户提供 和 只有一个虚拟对象 测试有很多测量 测量有一个测试
public class Test
{
public int Id { get; set; }
public string Testname { get; set; }
public virtual ICollection<Measurement> Measurements { get; set; }
}
public class Measurement
{
public int Id { get; set; }
public int FullMeasurement { get; set; }
public virtual Test Test { get; set; }
}
该表转到TestRefId进行标识测试
对不起,我说的是英语你是在要求代码审查吗?不是真正的代码审查,而是一种策略。可能想让测试和测量属性虚拟化。那会怎么样?谢谢
public class Test
{
public int Id { get; set; }
public string Testname { get; set; }
public virtual ICollection<Measurement> Measurements { get; set; }
}
public class Measurement
{
public int Id { get; set; }
public int FullMeasurement { get; set; }
public virtual Test Test { get; set; }
}
[ForeignKey("TestRefId")]
public virtual Test Test { get; set; }