entity framework mysql guid,c# - Entity Framework 6 GUID作为主键:不能将值NULL插入列'Id',表'FileStore'; 列不允许空值...

这适用于我(没有Azure),dev服务器上的SQL 2008 R2或本地工作站上的localdb \ mssqllocaldb。 注意:实体添加了Create,CreateBy,Modified,ModifiedBy和Version列。

public class Carrier : Entity

{

public Guid Id { get; set; }

public string Code { get; set; }

public string Name { get; set; }

}

然后创建一个映射配置类

public class CarrierMap : EntityTypeConfiguration

{

public CarrierMap()

{

HasKey(p => p.Id);

Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

Property(p => p.Code)

.HasMaxLength(4)

.IsRequired()

.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute { IsClustered = true, IsUnique = true }));

Property(p => p.Name).HasMaxLength(255).IsRequired();

Property(p => p.Created).HasPrecision(7).IsRequired();

Property(p => p.Modified)

.HasColumnAnnotation("IX_Modified", new IndexAnnotation(new IndexAttribute()))

.HasPrecision(7)

.IsRequired();

Property(p => p.CreatedBy).HasMaxLength(50).IsRequired();

Property(p => p.ModifiedBy).HasMaxLength(50).IsRequired();

Property(p => p.Version).IsRowVersion();

}

}

当您像这样执行add-migration时,这会在初始DbMigration中创建一个Up方法

CreateTable(

"scoFreightRate.Carrier",

c => new

{

Id = c.Guid(nullable: false, identity: true),

Code = c.String(nullable: false, maxLength: 4),

Name = c.String(nullable: false, maxLength: 255),

Created = c.DateTimeOffset(nullable: false, precision: 7),

CreatedBy = c.String(nullable: false, maxLength: 50),

Modified = c.DateTimeOffset(nullable: false, precision: 7,

annotations: new Dictionary

{

{

"IX_Modified",

new AnnotationValues(oldValue: null, newValue: "IndexAnnotation: { }")

},

}),

ModifiedBy = c.String(nullable: false, maxLength: 50),

Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),

})

.PrimaryKey(t => t.Id)

.Index(t => t.Code, unique: true, clustered: true);

注意:Id列没有获得默认值,请不要担心

现在执行Update-Database,你最终应该在数据库中得到一个表定义,如下所示:

CREATE TABLE [scoFreightRate].[Carrier] (

[Id] UNIQUEIDENTIFIER DEFAULT (newsequentialid()) NOT NULL,

[Code] NVARCHAR (4) NOT NULL,

[Name] NVARCHAR (255) NOT NULL,

[Created] DATETIMEOFFSET (7) NOT NULL,

[CreatedBy] NVARCHAR (50) NOT NULL,

[Modified] DATETIMEOFFSET (7) NOT NULL,

[ModifiedBy] NVARCHAR (50) NOT NULL,

[Version] ROWVERSION NOT NULL,

CONSTRAINT [PK_scoFreightRate.Carrier] PRIMARY KEY NONCLUSTERED ([Id] ASC)

);

GO

CREATE UNIQUE CLUSTERED INDEX [IX_Code]

ON [scoFreightRate].[Carrier]([Code] ASC);

注意:我们重写了SqlServerMigrationSqlGenerator以确保它不会使主键成为聚簇索引,因为我们鼓励开发人员在表上设置更好的聚簇索引

public class OurMigrationSqlGenerator : SqlServerMigrationSqlGenerator

{

protected override void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation)

{

if (addPrimaryKeyOperation == null) throw new ArgumentNullException("addPrimaryKeyOperation");

if (!addPrimaryKeyOperation.Table.Contains("__MigrationHistory"))

addPrimaryKeyOperation.IsClustered = false;

base.Generate(addPrimaryKeyOperation);

}

protected override void Generate(CreateTableOperation createTableOperation)

{

if (createTableOperation == null) throw new ArgumentNullException("createTableOperation");

if (!createTableOperation.Name.Contains("__MigrationHistory"))

createTableOperation.PrimaryKey.IsClustered = false;

base.Generate(createTableOperation);

}

protected override void Generate(MoveTableOperation moveTableOperation)

{

if (moveTableOperation == null) throw new ArgumentNullException("moveTableOperation");

if (!moveTableOperation.CreateTableOperation.Name.Contains("__MigrationHistory")) moveTableOperation.CreateTableOperation.PrimaryKey.IsClustered = false;

base.Generate(moveTableOperation);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值