2013-02-14 56 views
0

我有这些类描述我的DB模式:ADO.NET EF如何设置FK与代码首先

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; } 
    } 

它的工作很好,但是当我尝试添加FK约束在App.Config这样的:

<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是否正确吗?

回答