使用随VS 2015 RC提供的EF 7的版本,当我在OnModelCreating()中设置它时,我的datacontext无法正确验证“必需”设置
以下是我的DbContext中的代码:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region Member
modelBuilder.Entity<Member>()
.ForRelational()
.Table("Member", "dbo");
modelBuilder.Entity<Member>()
.Key(x => x.Id);
modelBuilder.Entity<Member>()
.Index(x => x.UserName)
.Unique();
modelBuilder.Entity<Member>()
.Property(x => x.UserName)
.Required();
modelBuilder.Entity<Member>()
.Index(x => x.Moniker)
.Unique();
modelBuilder.Entity<Member>()
.Property(x => x.Moniker)
.MaxLength(128)
.Required();
modelBuilder.Entity<Member>()
.Index(x => x.Email)
.Unique();
modelBuilder.Entity<Member>()
.Property(x => x.Email)
.Required();
modelBuilder.Entity<Member>()
.Property(x => x.Email)
.MaxLength(256);
modelBuilder.Entity<Member>()
.Property(x => x.LastName)
.MaxLength(256);
modelBuilder.Entity<Member>()
.Property(x => x.FirstName)
.MaxLength(256);
modelBuilder.Entity<Member>()
.Property(x => x.Middle)
.MaxLength(256);
modelBuilder.Entity<Member>()
.Property(x => x.ConcurrencyStamp)
.ConcurrencyToken()
.StoreComputed();
#endregion
modelBuilder.Entity<MemberRole>()
.Key(k => new
{
k.MemberId,
k.RoleId
});
modelBuilder.Entity<MemberRole>()
.ForRelational()
.Table("MemberRole", "dbo");
modelBuilder.Entity<MemberRole>()
.Property(x => x.ConcurrencyStamp)
.ConcurrencyToken()
.StoreComputed();
modelBuilder.Entity<MemberRole>()
.Reference(m => m.Member)
.InverseCollection(x => x.Roles)
.ForeignKey(x=>x.RoleId);
modelBuilder.Entity<MemberRole>()
.Reference(m => m.Role)
.InverseCollection(x => x.Members)
.ForeignKey(x => x.MemberId);
#region Role
modelBuilder.Entity<Role>()
.Key(k => k.Id);
modelBuilder.Entity<Role>()
.ForRelational()
.Table("Role", "dbo");
modelBuilder.Entity<Role>()
.Property(x => x.ConcurrencyStamp)
.ConcurrencyToken()
.StoreComputed();
//modelBuilder.Entity<Role>()
// .Collection(r=>r.Members)
// .InverseReference(m=>m.Role).ForeignKey(x=>x.RoleId);
#endregion
#region MemberClaim
modelBuilder.Entity<MemberClaim>()
.Key(k=>k.Id);
modelBuilder.Entity<MemberClaim>()
.ForRelational()
.Table("MemberClaim", "dbo");
modelBuilder.Entity<MemberClaim>()
.Reference(c => c.Member)
.InverseCollection(m => m.Claims)
.ForeignKey(c=>c.MemberId);
#endregion
base.OnModelCreating(modelBuilder);
}
我正在尝试为“代码优先”做数据库第一个选项,我已经使用了一段时间,因为它刚出现并且以前没有遇到过这个问题。
这是我在SQL中的数据表:
USE [Fambase]
GO
/****** Object: Table [dbo].[Member] Script Date: 5/17/2015 11:59:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Member](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[Moniker] [nvarchar](256) NOT NULL,
[UserName] [nvarchar](256) NOT NULL,
[TwoFactorEnabled] [bit] NOT NULL,
[AccessFailedCount] [int] NOT NULL,
[LockoutEnabled] [bit] NOT NULL,
[PhoneNumberConfirmed] [bit] NOT NULL,
[EmailConfirmed] [bit] NOT NULL,
[NormalizedUserName] [nvarchar](256) NULL,
[PasswordHash] [nvarchar](max) NULL,
[Email] [nvarchar](256) NULL,
[NormalizedEmail] [nvarchar](256) NULL,
[PhoneNumber] [varchar](50) NULL,
[LastName] [nvarchar](256) NULL,
[FirstName] [nvarchar](256) NULL,
[Middle] [nvarchar](256) NULL,
[LockoutEnd] [datetimeoffset](7) NULL,
[IsDeleted] [bit] NOT NULL,
[IsArchived] [bit] NOT NULL,
[DateTimeCreated] [datetime] NOT NULL,
[DateTimeModified] [datetime] NULL,
[DateTimeDeleted] [datetime] NULL,
[DateTimeArchived] [datetime] NULL,
[CreatedBy] [nvarchar](256) NOT NULL,
[ModifiedBy] [nvarchar](256) NULL,
[DeletedBy] [nvarchar](256) NULL,
[ArchivedBy] [nvarchar](256) NULL,
[SecurityStamp] [uniqueidentifier] NULL,
[ConcurrencyStamp] [timestamp] NOT NULL,
CONSTRAINT [PK_Member] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Member] ADD CONSTRAINT [DF_Member_Id] DEFAULT (newsequentialid()) FOR [Id]
GO
如果我在SQL服务器上删除allow null设置,则EF根本不会对其进行评估。如果设置为NO NULL,我会得到您希望从SQL中获得的SQL错误,但EF不会捕获它并将其抛出异常。
这似乎不像预期的那样。
UPDATE 我的基类如下
public class MemberBase : IMemberBase
{
public Guid Id { get; set; }
public string UserName { get; set; }
public string NormalizedUserName { get; set; }
public string PasswordHash { get; set; }
public string Email { get; set; }
public string NormalizedEmail { get; set; }
public bool EmailConfirmed { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public string Moniker { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Middle { get; set; }
public int AccessFailedCount { get; set; }
public bool LockoutEnabled { get; set; }
public DateTimeOffset? LockoutEnd { get; set; }
public Guid? SecurityStamp { get; set; }
public bool TwoFactorEnabled { get; set; }
}
实体继承自基类,如下所示:
public class Member : MemberBase, IMember
{
#region IAuditable
public bool IsDeleted { get; set; }
public bool IsArchived { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public string DeletedBy { get; set; }
public string ArchivedBy { get; set; }
public DateTime DateTimeCreated { get; set; }
public DateTime? DateTimeModified { get; set; }
public DateTime? DateTimeDeleted { get; set; }
public DateTime? DateTimeArchived { get; set; }
public byte[] ConcurrencyStamp { get; set; }
#endregion
#region Navigation
public virtual ICollection<ExternalLogin> ExternalLogins { get; set; } = new List<ExternalLogin>();
public virtual ICollection<MemberClaim> Claims { get; set; } = new List<MemberClaim>();
public virtual ICollection<MemberRole> Roles { get; set; }
#endregion
}