相关疑难解决方法(0)

代码优先:独立协会与外国关键协会?

每次我开始研究一个新项目时,我都会和自己进行一场心理辩论,而我正在设计我的POCO.我看过很多教程/代码示例似乎都支持外键关联:

外键关联

public class Order
{
    public int ID { get; set; }
    public int CustomerID { get; set; } // <-- Customer ID
    ...
}
Run Code Online (Sandbox Code Playgroud)

独立协会相反:

独立协会

public class Order
{
    public int ID { get; set; }
    public Customer Customer { get; set; } // <-- Customer object
    ...
}
Run Code Online (Sandbox Code Playgroud)

我以前使用过NHibernate,并且使用了独立的关联,它们不仅感觉更多OO,而且(延迟加载)的优势在于可以让我访问整个Customer对象,而不仅仅是ID.例如,这允许我检索Order实例,然后Order.Customer.FirstName无需显式地进行连接,这非常方便.

所以回顾一下,我的问题是:

  1. 使用独立关联有任何明显的缺点吗?和...
  2. 如果没有,那么使用外键关联的原因是什么?

entity-framework poco

100
推荐指数
3
解决办法
3万
查看次数

实体框架将导航属性设置为null

我有一个实体框架数据库第一个项目.这是模型的提取:

public partial class LedProject
{
    public LedProject()
    {
        this.References = new HashSet<LedProjectReference>();
        this.Results = new HashSet<LedProjectResult>();
        this.History = new HashSet<LedProjectHistory>();
    }

    public string Identifier { get; set; }
    public string Name { get; set; }
    public Nullable<System.DateTime> CompletionDate { get; set; }
    public System.DateTime CreationDate { get; set; }
    public System.Guid ProjectId { get; set; }
    public string Comment { get; set; }

    public virtual User ContactUser { get; set; }
    public virtual User CreationUser { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

21
推荐指数
2
解决办法
1万
查看次数

使用Entity Framework Code First时创建外键属性有什么意义?

在查看本网站上的问题和答案并阅读一些关于Code First开发的Google排名前列的教程时,我经常会看到以下模式......

public class Category
{
    public Category()
    {
        Products = new Collection<Product>();
    }
    public Guid ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

public class Product
{
    public Guid ID { get; set; }
    public string Name { get; set; }
    public DateTime DateAdded { get; set; }
    public Guid CategoryID { get; set; } // Seemingly redundant property
    public virtual Category Category { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first

9
推荐指数
1
解决办法
1271
查看次数

标签 统计

entity-framework ×3

c# ×2

ef-code-first ×1

poco ×1