C# 这取决于您采取的方法。使用代码优先的方法时,将DatabaseGenerated属性添加到键字段中。采用数据库优先的方法时,在模型上显式地将StoredGeneratedPattern设置为Identity [1]: https://i.stack.img
这取决于您采取的方法。使用代码优先的方法时,将DatabaseGenerated属性添加到键字段中。采用数据库优先的方法时,在模型上显式地将StoredGeneratedPattern设置为IdentityC# 这取决于您采取的方法。使用代码优先的方法时,将DatabaseGenerated属性添加到键字段中。采用数据库优先的方法时,在模型上显式地将StoredGeneratedPattern设置为Identity [1]: https://i.stack.img,c#,entity-framework,sql-server-2008,entity-framework-6,C#,Entity Framework,Sql Server 2008,Entity Framework 6,这取决于您采取的方法。使用代码优先的方法时,将DatabaseGenerated属性添加到键字段中。采用数据库优先的方法时,在模型上显式地将StoredGeneratedPattern设置为Identity [1]: https://i.stack.imgur.com/IxGdd.png [2]: https://i.stack.imgur.com/Qssea.png 您是否尝试将注释[DatabaseGenerated(DatabaseGeneratedOption.Identity)]放在
[1]: https://i.stack.imgur.com/IxGdd.png
[2]: https://i.stack.imgur.com/Qssea.png
您是否尝试将注释
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
放在公共Guid ID{get;set;}
之前?是否在表的初始生成之后添加了配置?在Ikian中,我认为fluent-api是首选,因为这里覆盖了OnModelCreating
。我发现您还没有接受任何答案。你对什么都不满意吗?如果是这样,让我知道,我会发布另一个,这是有效的。只是觉得有点懒,不想输入,若我并没有得到代表:)谢谢你们的回答,但我在项目中首先使用代码。所以我不想手动创建数据库。@阿尔及尔达斯为什么刚才在每个实体的构造函数中都有一个Guid.NewGuid()。我没有找到任何其他解决方案,所以我在构造函数中添加了Guid.NewGuid()。Thanks@Algirdas仅供参考,Guid.NewGuid()不会索引数据库中的内容。您应该使用顺序Guid,但是我认为通过覆盖OnModelCreating,您可以像使用此属性一样设置相同的设置。我来读这篇文章。感谢如果它是主键
,则该字段中不可能有重复的guid键。因为主键
将具有唯一的约束。数据库服务器将拒绝重复的主键。该Azure便笺确实为我节省了一些时间。谢谢。自Azure SQL V12以来,newsequentialid()一直受支持。这是什么?我们在哪里使用它?“Id=c.Guid(nullable:false,identity:true,defaultValueSql:”newsequentialid()”,
“我知道已经有一段时间了,但是对于未来的读者,Id赋值语句会出现在codefirst迁移文件中。仅供参考:在ef7 RC1工作中[Key,DatabaseGenerated(DatabaseGeneratedOption.identity)]很好。。。但也只对新数据库进行了测试…这会创建碎片,对于小数据库来说这不是问题,但是大数据库应该使用顺序数据库guid@Miroslav,我假设您谈论的是我上面的答案,如果是,则不添加构造函数,当实体保存在数据库中时,数据库将创建一个顺序guid。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<FileStore>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
base.OnModelCreating(modelBuilder);
}
public class FileStore
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string Name { get; set; }
public string Path { get; set; }
}
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
[Id] [uniqueidentifier] NOT NULL DEFAULT newsequentialid(),
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
Id = c.Guid(nullable: false, identity: true, defaultValueSql: "newsequentialid()"),
public class Carrier : Entity
{
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
public class CarrierMap : EntityTypeConfiguration<Carrier>
{
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();
}
}
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<string, AnnotationValues>
{
{
"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);
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);
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);
}
}
public class Carrier : Entity
{
public Carrier()
{
this.Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
public override void Up()
{
AlterColumn("dbo.MyTable","Id", c => c.Guid(nullable: false, identity: true, defaultValueSql: "newsequentialid()"));
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
codefirst-defaultvalue
databasefirst-model
using (ApplicationDbContext context = new ApplicationDbContext())
{
var person = new Person
{
FirstName = "Random",
LastName = "Person";
};
context.People.Add(person);
context.SaveChanges();
Console.WriteLine(person.Id);
}
[1]: https://i.stack.imgur.com/IxGdd.png
[2]: https://i.stack.imgur.com/Qssea.png