型号:
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; }
}
的DbContext:
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玩的第一天,我只是想知道如何能够加入这两张桌子。我如何定义关系?
此外,有关流畅API的任何教程都对您有帮助吗?
答案 0 :(得分:1)
您可以使用下一个代码示例:
modelBuilder.Entity<User>()
.HasRequired(u=>u.Membership)
.WithOptional()
.HasForeignKey(u=>u.UserId)