实体框架4.1中的外键

时间:2021-07-03 11:30:29

I am working on Entity Framework 4.1 and using data annotations for foreign keys. I want to know how can we define one to many relationship between product and categories. I want to map category. categoryId with product.cid

我正在使用Entity Framework 4.1并使用外键的数据注释。我想知道如何定义产品和类别之间的一对多关系。我想要分类。 categoryId with product.cid

public class Category
{
    public string CategoryId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string CId { get; set; }

    public virtual Category Category { get; set; }
} 

Please suggest

请建议

2 个解决方案

#1


24  

Both these approaches should work:

这两种方法都应该有效:

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    [ForeignKey("Category")]
    public string CId { get; set; }

    public virtual Category Category { get; set; }
}

Or:

要么:

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string CId { get; set; }

    [ForeignKey("CId")]
    public virtual Category Category { get; set; }
} 

ForeignKeyAttribute is used to pair navigation property and foreign key property. It contains either the name of related navigation property or the name of related foreign key property.

ForeignKeyAttribute用于配对导航属性和外键属性。它包含相关导航属性的名称或相关外键属性的名称。

#2


6  

You use entityconfigurations for this.

您可以使用实体配置。

In the entityconfiguration, add this mapping to the category entityconfiguration:

在实体配置中,将此映射添加到类别实体配置:

HasMany<Product>(x => x.Products).WithRequired().HasForeignKey(x => x.CId);

You only need to map it on one of your classes and DbContext is clever enough to use it.

您只需要在其中一个类上映射它,DbContext就足够聪明地使用它。

See this blog post for more info.

有关详细信息,请参阅此博客文章。

EDIT To do this using data annotations I believe the correct approach is as follows:

编辑为了使用数据注释做到这一点我相信正确的方法如下:

[RelatedTo(RelatedProperty="Products", Key="CId", RelatedKey="CategoryId")]
public virtual Category Category { get; set; }

#1


24  

Both these approaches should work:

这两种方法都应该有效:

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    [ForeignKey("Category")]
    public string CId { get; set; }

    public virtual Category Category { get; set; }
}

Or:

要么:

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string CId { get; set; }

    [ForeignKey("CId")]
    public virtual Category Category { get; set; }
} 

ForeignKeyAttribute is used to pair navigation property and foreign key property. It contains either the name of related navigation property or the name of related foreign key property.

ForeignKeyAttribute用于配对导航属性和外键属性。它包含相关导航属性的名称或相关外键属性的名称。

#2


6  

You use entityconfigurations for this.

您可以使用实体配置。

In the entityconfiguration, add this mapping to the category entityconfiguration:

在实体配置中,将此映射添加到类别实体配置:

HasMany<Product>(x => x.Products).WithRequired().HasForeignKey(x => x.CId);

You only need to map it on one of your classes and DbContext is clever enough to use it.

您只需要在其中一个类上映射它,DbContext就足够聪明地使用它。

See this blog post for more info.

有关详细信息,请参阅此博客文章。

EDIT To do this using data annotations I believe the correct approach is as follows:

编辑为了使用数据注释做到这一点我相信正确的方法如下:

[RelatedTo(RelatedProperty="Products", Key="CId", RelatedKey="CategoryId")]
public virtual Category Category { get; set; }