【发布时间】:2015-03-06 12:25:25
【问题描述】:
我在 EntityFramework6 项目中的数据库设计有一点问题。这是我用来创建数据库的两个代码优先类。正如您所看到的,Details 类违反了所有正常形式,我想正确地拆分它,但是我不知道如何正确地做到这一点,因为我对 SQLServer 和 EF 完全陌生。 那么,获得一个体面的数据库设计以便我可以查询的最佳方法是什么?
internal class LocationDataContract
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DbGeography Coordinates { get; set; }
public LocationsDetailsDataContract Details { get; set; }
}
internal class LocationDetailsDataContract
{
[Key, ForeignKey("Location")]
public Guid Id { get; set; }
public string Planet { get; set; }
public bool? IsCapital { get; set; }
public int Population { get; set; }
public string System { get; set; }
public string Sector { get; set; }
public LocationDataContract Location { get; set; }
}
以下更新问题
所以按照乔治的建议,我做了以下事情。这会是一个不错的解决方案。我必须补充一点,我需要进行一些转换,因为应用程序使用不同的(完全基于继承的)模型,我必须使用这些模型,因为数据库不是唯一的数据源,并且所述数据还使用 另一个 em> 格式来存储我无法更改的数据。
internal class LocationDataContract
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DbGeography Coordinates { get; set; }
public LocationsDetails Details { get; set; }
}
internal abstract class LocationDetailDataContract
{
[Key, ForeignKey("Location")]
public Guid Id { get; set; }}
public LocationsDetails Details { get; set; }
}
internal class CityDetailDataContract : LocationDetailDataContract
{
public string Planet { get; set; }
public bool? IsCapital { get; set; }
public int Population { get; set; }
}
internal class PlantDetailDataContract : LocationDetailDataContract
{
public int Population { get; set; }
public string System { get; set; }
}
internal class SystemDetailDataContract : LocationDetailDataContract
{
public string Sector { get; set; }
}
internal class SectorDetailDataContract : LocationDetailDataContract
{
// For now nothing
}
【问题讨论】:
-
为什么不为 Planet、System 和 Sector 等属性创建新类来规范数据库?
-
@George 我更新了原始问题。这是你的意思吗?
-
看看我的答案是什么意思......
标签: c# sql sql-server entity-framework ef-code-first