Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 型号上的外键(代码优先)_Asp.net Mvc 3_Entity Framework - Fatal编程技术网

Asp.net mvc 3 型号上的外键(代码优先)

Asp.net mvc 3 型号上的外键(代码优先),asp.net-mvc-3,entity-framework,Asp.net Mvc 3,Entity Framework,我有一些模型和数据库,但没有外键将任何东西链接在一起。这似乎是我的项目中的一个巨大弱点,所以我尝试将外键插入到我的模型中,然后根据我的模型重新生成数据库 我很难理解外键是如何工作的,尤其是在一对多的关系中 在本例中,我有一个或多个产品,每个产品可能有多个评论: 我删除了一些注释和属性来压缩模型 产品型号/实体: public class Product { public int ProductId { get; set; } public int Own

我有一些模型和数据库,但没有外键将任何东西链接在一起。这似乎是我的项目中的一个巨大弱点,所以我尝试将外键插入到我的模型中,然后根据我的模型重新生成数据库

我很难理解外键是如何工作的,尤其是在一对多的关系中

在本例中,我有一个或多个产品,每个产品可能有多个评论:

我删除了一些注释和属性来压缩模型

产品型号/实体:

public class Product
    {
        public int ProductId { get; set; }

        public int OwnerId {get; set; }

        public string Title { get; set; }

        public int Rating { get; set; }

        public decimal Price { get; set; }

        public int Quantity { get; set; }

        public string Description { get; set; }
    }
 public class Review
    {

        public int ReviewId { get; set; }

        public int ProductId { get; set; }

        public int WriterId { get; set; }

        public string Title { get; set; }

        public string Body { get; set; }
    }
审查模型/实体:

public class Product
    {
        public int ProductId { get; set; }

        public int OwnerId {get; set; }

        public string Title { get; set; }

        public int Rating { get; set; }

        public decimal Price { get; set; }

        public int Quantity { get; set; }

        public string Description { get; set; }
    }
 public class Review
    {

        public int ReviewId { get; set; }

        public int ProductId { get; set; }

        public int WriterId { get; set; }

        public string Title { get; set; }

        public string Body { get; set; }
    }

我希望对Product的ProductId和review的ProductId进行外键约束。如何使其工作?

外键的作用是将表链接在一起。例如,当存在唯一的id号时,外键很容易解释。productId应该是唯一的id,并且可能是产品表的主键。在review表中,productId是一个外键,因为它将允许联接表并查看两个表中所有类别的数据

从产品中选择ProductId、标题、价格、WriterId、描述作为p,查看作为R 其中P.ProductId=R.ProductId

当对ur db运行此select语句时,您将看到审查表DEScript中的产品唯一id、标题、价格、编写器id。关键行是P.ProductId=R.ProductId


还要确保外键不为空

外键的目的是将表链接在一起。例如,当存在唯一的id号时,外键很容易解释。productId应该是唯一的id,并且可能是产品表的主键。在review表中,productId是一个外键,因为它将允许联接表并查看两个表中所有类别的数据

从产品中选择ProductId、标题、价格、WriterId、描述作为p,查看作为R 其中P.ProductId=R.ProductId

当对ur db运行此select语句时,您将看到审查表DEScript中的产品唯一id、标题、价格、编写器id。关键行是P.ProductId=R.ProductId


另外,请确保外键不为null,您至少需要一个导航属性来定义两个实体之间的关系,例如:

public class Review
{
    public int ReviewId { get; set; }

    [ForeignKey("Product")]
    public int ProductId { get; set; }
    public Product Product { get; set; }

    public int WriterId { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
}
如果需要,还可以在
产品中添加集合属性:

public ICollection<Review> Reviews { get; set; }

至少需要一个导航属性来定义两个实体之间的关系,例如:

public class Review
{
    public int ReviewId { get; set; }

    [ForeignKey("Product")]
    public int ProductId { get; set; }
    public Product Product { get; set; }

    public int WriterId { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
}
如果需要,还可以在
产品中添加集合属性:

public ICollection<Review> Reviews { get; set; }

modelBuilder在ModelCreating中有详细说明,对吗?目前我使用的是
modelBuilder.Entity().ToTable(“Reviews”,schemaName:“mySchema”)我需要根据我的代码重新生成数据库,对吗?@Johannes:是的,在
OnModelCreating
中。当前映射仅将实体映射到表名。您可以添加更多映射代码,只需重复
modelBuilder.Entity()。您不需要重新生成数据库,还可以在数据库模式和模型中进行“并行”更改,以便匹配并关闭数据库初始值设定项(
Database.SetInitializer(null)
),如果数据库正在生产中,通常是必要的。如果您处于开发阶段,那么让EF重新生成DB模式会有所帮助,因为它可以确保它与模型匹配。或者您可以使用EF 4.3“迁移”功能。modelBuilder在ModelCreating中有详细说明,对吗?目前我使用的是
modelBuilder.Entity().ToTable(“Reviews”,schemaName:“mySchema”)我需要根据我的代码重新生成数据库,对吗?@Johannes:是的,在
OnModelCreating
中。当前映射仅将实体映射到表名。您可以添加更多映射代码,只需重复
modelBuilder.Entity()。您不需要重新生成数据库,还可以在数据库模式和模型中进行“并行”更改,以便匹配并关闭数据库初始值设定项(
Database.SetInitializer(null)
),如果数据库正在生产中,通常是必要的。如果您处于开发阶段,那么让EF重新生成DB模式会有所帮助,因为它可以确保它与模型匹配。或者您可以使用EF 4.3“迁移”功能。