0

模型:

public class User {
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public virtual Membership Membership { get; set; }
}

public class Membership {
    public Guid UserId { get; set; }
    public DateTime CreateDate { get; set; }
}

数据库上下文:

public class UsersContext : DbContext {
    public UsersContext() : base("ApplicationServices") { }
    public DbSet<User> Users { get; set; }
    public DbSet<Membership> Memberships { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Entity<User>().ToTable("aspnet_Users");
        modelBuilder.Entity<Membership>().ToTable("aspnet_Membership");

        //insert relation here to join the two tables
    }
}

这是我第一天玩流利的 api,我只是想知道如何才能加入这两个表。我将如何定义这种关系?

另外,关于 fluent API 的任何教程对您有帮助吗?

4

1 回答 1

1

您可以使用下一个代码示例:

    modelBuilder.Entity<User>()
                .HasRequired(u=>u.Membership)
                .WithOptional()
                .HasForeignKey(u=>u.UserId)
于 2012-07-29T12:33:00.047 回答