2013-02-14 61 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是否正確嗎?

回答