0

我有这些描述我的数据库模型的类:

 public class Blog
    {
        public int BlogId { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }

        public virtual List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public class User
    {
        public int UserId { get; set; }
        public string Username { get; set; }
        public string DisplayName { get; set; }
    }

    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<User> Users { get; set; }
    }

它工作正常,但是当我尝试App.Config像这样添加 FK 约束时:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlClient" />
      </parameters>
    </defaultConnectionFactory>
    <Association Name="UserBlogs">
      <End Type="CodeFirstNewDatabaseSample.BloggingContext.User" Role="User" Multiplicity="1" >
        <OnDelete Action="Cascade" />
      </End>
      <End Type="CodeFirstNewDatabaseSample.BloggingContext.Blog" Role="Blog" Multiplicity="*" />
      <ReferentialConstraint>
        <Principal Role="User">
          <PropertyRef Name="UserId" />
        </Principal>
        <Dependent Role="Blog">
          <PropertyRef Name="UserId" />
        </Dependent>
      </ReferentialConstraint>
    </Association>
  </entityFramework>

我得到一个错误App.Config has threw an error。如何使用此代码作为示例添加 FKeys,是否App.Config适合这样做?

4

1 回答 1

1

模型构建器是我使用 Entity Framework 配置 FK 的首选方法

查看我的博客文章,其中详细介绍了如何在此处执行此操作:http: //blog.staticvoid.co.nz/2012/7/17/entity_framework-navigation_property_basics_with_code_first

于 2013-02-14T09:26:11.867 回答