2013-02-14 3 views
0

У меня есть эти классы, которые описывают мою БД модель:ADO.NET EF, как установить FK с Code First

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 - подходящее место для этого?

ответ

Смежные вопросы