【问题标题】:Properly splitting up code for SQL Database in CodeFirst在 Code First 中正确拆分 SQL 数据库的代码
【发布时间】: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


【解决方案1】:

@Ruhrpottpatriot,为什么所有类都继承自 LocationDetailDataContract? 这样做我相信您将拥有大量类和表,但数据将保持非规范化(例如:CityDetailDataContract 将包含带有重复字符串的 Planet 列。

你可以从你原来的问题中做这样的事情吗?

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 LocationDetailsDataContract Details { get; set; }
}

internal class LocationDetailsDataContract
{
    [Key, ForeignKey("Location")]
    public Guid Id { get; set; }

    public Planet Planet { get; set; }

    public System System { get; set; }

    public Sector Sector { get; set; }

    [Required]
    public LocationDataContract Location { get; set; }
}

internal class Planet {
    [Key]
    public Guid Id { get; set; }

    public string Name { get; set; }

    public bool? IsCapital { get; set; }

    public int Population { get; set; }
}

internal class System {
    [Key]
    public Guid Id { get; set; }

    public string Name { get; set; }
}
internal class Sector {
    [Key]
    public Guid Id { get; set; }

    public string Name { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    相关资源
    最近更新 更多