我首先使用模型。
实体:
class User
{
Guid UserId{get;set;}
Guid RefererUserId {get;set;}
User RefereUser {get;set;}
}
数据引文 :
public class DataContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasRequired(x => x.ReferrerUser)
.WithMany()
.HasForeignKey(x => x.ReferrerUserId).WillCascadeOnDelete(false);
}
public DbSet<User> Users { get; set; }
}
利用数据库:
MembershipCreateStatus Status;
Membership.CreateUser("Demo", "123456", "[email protected]", null, null, true, out Status);
Roles.CreateRole("Admin");
Roles.AddUserToRole("Demo", "Admin");
DataContext db = new DataContext();
var a = db.Users.Where(x => x.Email == "[email protected]").FirstOrDefault();
a.MoneyRUB = 100;
a.MoneyUSD = 100;
db.SaveChanges();
我得到错误 :
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "User_ReferrerUser". The conflict occurred in database "Kamikaze", table "dbo.Users", column UserId . The statement has been terminated.
我做错什么了?