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
Sql 如果我不';在EF中不显式使用外键?_Sql_Entity Framework_Entity Framework 5_Entity Framework 6 - Fatal编程技术网

Sql 如果我不';在EF中不显式使用外键?

Sql 如果我不';在EF中不显式使用外键?,sql,entity-framework,entity-framework-5,entity-framework-6,Sql,Entity Framework,Entity Framework 5,Entity Framework 6,我正在做我的主要项目。我遇到了一个问题,其中我有多对多关系: “用户”表和“博客帖子”表 “用户”表和“标签”表。 我使用的是实体框架6.0 这是我的密码 public class BlogPost { public int Id { get; set; } public string Text { get; set; } public ICollection<User> Users { get; set; } } public class Tag {

我正在做我的主要项目。我遇到了一个问题,其中我有多对多关系:

  • “用户”表和“博客帖子”表
  • “用户”表和“标签”表。 我使用的是实体框架6.0
  • 这是我的密码

    public class BlogPost
    {
        public int Id { get; set; }
        public string Text { get; set; }
    
        public ICollection<User> Users { get; set; }
    }
    
    public class Tag
    {
        public int Id { get; set; }
        public string TagName { get; set; }
    
        public ICollection<User> Users { get; set; }
    }
    
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    
        public ICollection<Tag> Intrests { get; set; }
    
        public ICollection<BlogPost> BlogPosts { get; set; }
    }
    
    公共类BlogPost
    {
    公共int Id{get;set;}
    公共字符串文本{get;set;}
    公共ICollection用户{get;set;}
    }
    公共类标签
    {
    公共int Id{get;set;}
    公共字符串标记名{get;set;}
    公共ICollection用户{get;set;}
    }
    公共类用户
    {
    公共int Id{get;set;}
    公共字符串名称{get;set;}
    公共整数{get;set;}
    公共ICollection intrest{get;set;}
    公共ICollection BlogPosts{get;set;}
    }
    
    如果我使用上述代码,我会得到预期的模式。即 我有五张桌子。用户,BlogPost,标记,标记用户,UserBlogPost

    其中用户仅包含:Id、名称和年龄。 TagUser和UserBlogPost包含用户的主键和它们自己的表,并在它们之间创建多对多关系

    如果我显式地为我的所有导航属性创建外键属性,如下所示:

    public class BlogPost
    {
        public int Id { get; set; }
        public string Text { get; set; }
    
        public int UserId { get; set; }
        public ICollection<User> Users { get; set; }
    }
    
    public class Tag
    {
        public int Id { get; set; }
        public string TagName { get; set; }
    
        public int UserId { get; set; }
        public ICollection<User> Users { get; set; }
    }
    
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    
        public int TagId { get; set; }
        public ICollection<Tag> Intrests { get; set; }
    
        public int BlogPostId { get; set; }
        public ICollection<BlogPost> BlogPosts { get; set; }
    } 
    
    公共类BlogPost
    {
    公共int Id{get;set;}
    公共字符串文本{get;set;}
    public int UserId{get;set;}
    公共ICollection用户{get;set;}
    }
    公共类标签
    {
    公共int Id{get;set;}
    公共字符串标记名{get;set;}
    public int UserId{get;set;}
    公共ICollection用户{get;set;}
    }
    公共类用户
    {
    公共int Id{get;set;}
    公共字符串名称{get;set;}
    公共整数{get;set;}
    public int TagId{get;set;}
    公共ICollection intrest{get;set;}
    public int BlogPostId{get;set;}
    公共ICollection BlogPosts{get;set;}
    } 
    
    我得到了不同的结果。 这里的用户表包含:Id、Name、Age和TagId,以及BlogPostId。 标记和BlogPost表包含引用用户表的外键。 它还有TagUser和UserBlogPost表

    所以,如果我不在类中创建外键属性,那么是否有任何问题。我如何处理这种情况?
    请帮助…

    用户ID字段的功能是什么?@GertArnold:我使用用户ID来唯一标识每个用户。它被用作Tag和BlogPost中的外键。我的意思是,除了这些,它不提供任何其他功能。当然,但为什么你需要这些钥匙?他们的角色是什么?他们与
    用户
    集合完全不同。@GertArnold:先生,我正在创建一个博客,还有其他的类。在这里,一个用户可以发布多篇博客文章(BlogPost),一个用户可以有多个标签(标签类似于兴趣),多个用户可以有相同的标签。好的,最后一次尝试。为什么
    标签
    需要引用一个用户?正如您所说,标记有多个用户。我觉得您不明白在将
    UserId
    (或
    TagId和
    BlogPostId`添加到
    User
    )时您在做什么。