自动递增数字的最佳实践 EF Core
Best practices for auto increment a number EF Core
我正在寻找最佳实践,以便使用 EF Core 在我的数据库中增加一些数字..
我有一个名为 PARENT 的数据库模型。在我的应用程序中,我可以有几个 PARENTS。每个 PARENT 有几个孩子。
PARENT1:孩子 1 - 孩子 2
PARENT2:孩子 1 - 孩子 2 - 孩子 3
我希望在每个 PARENT 中,每个 children 都按一列递增编号:int Number
。这意味着,
PARENT1.CHILD1 = 001;
PARENT1.CHILD2 = 002;
PARENT2.CHILD1 = 001;
PARENT2.CHILD2 = 002;
...
所以,它只能是唯一的,并且递增 parent..
任何人都可以给我一些最佳实践以有效地实现这一目标吗?
更新:
public class Bar {
public Guid BarId {get;set;}
public ICollection<Client> Clients {get;set;}
}
public class Client {
public Guid ClientId {get;set;}
public Guid BarId {get;set;}
public int ClientNumber {get;set;}
}
public class Context : DBContext {
entity.HasKey(e => new { e.BarId, e.ClientId }).HasName("PK_CLIENT");
entity.Property(e => e.ClientId).ValueGeneratedWhenDefault();
entity.Property(e => e.ClientNumber).ValueGeneratedOnAdd();
entity.HasIndex(e => new {e.BarId, e.ClientNumber}).IsUnique()
.HasName("IX_CLIENT_NUMBER");
}
通过调用 ValueGeneratedOnAdd()
我使数字列递增,但我希望它为每个 Bar 递增,因此它应该允许不同 bar 之间重复的客户编号,但不能在同一个 Bar 上,并且也递增他们由 Bar,而不仅仅是客户实体。
也许这不是你想要的,但由于我正在编写一些代码,所以它不适合作为评论。
要在子组合中设置 ClientNumber,我会编写如下代码:
Bar bar = new Bar();
int seq = 1;
bar.Clients.Add(new Client() { ClientNumber = seq++; });
bar.Clients.Add(new Client() { ClientNumber = seq++; });
dbContext.Update(bar);
dbContext.SaveChanges();
创建新栏时它工作正常。如果它可以被编辑并且客户端可以被添加或从栏中删除 - 就像一个 CRUD - 那么你必须改变一些东西。但它显示了我在评论中谈论的内容(设置 ClientNumber)。
另一种方法是拦截 SaveChanges,在这种情况下我会避免这样做,因为对于不明智的开发人员来说这有点不清楚:
public class DbContextWithBlackMage : DbContext
{
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
OnBeforeSaving();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
private void OnBeforeSaving()
{
var entries = ChangeTracker.Entries();
foreach (var entry in entries)
{
//TODO black mage to fill the ClientNumber very secretly and obscurely
//TODO Matheus will be angry when he finds out muahahahahaha!
}
}
}
无论如何...这可能不是您想要的,但也许它会激发人们对如何解决(或如何不解决)的意愿。
一种方法是执行以下操作;
1) 使用整数作为键,并设置 ClientNumber 属性 可以为空。
public class Bar
{
public int BarId { get; set; }
public ICollection<Client> Clients { get; set; }
}
public class Client
{
public int ClientId { get; set; }
public Bar Bar { get; set; }
public int BarId { get; set; }
public int? ClientNumber { get; set; }
}
2) 设置ClientId为identity(自动递增),ClientNumber为数据库生成的(否则EF Core插入后不会从数据库中重新读取值)
entity.Property(e => e.ClientId).ValueGeneratedOnAdd();
entity.Property(e => e.ClientNumber).ValueGeneratedOnAddOrUpdate();
3) 插入后添加修改ClientNumber的触发器
create trigger ClientNumber on Clients after insert
as
begin
set nocount on;
update c set ClientNumber = n.RowNum from Clients c
inner join (
select ClientId, ROW_NUMBER() OVER(PARTITION BY BarId ORDER BY ClientId ASC) as RowNum
from Clients
) n on n.ClientId = c.ClientId
where c.ClientId in (select ClientId from inserted)
end
现在一切都自动插入:
var bar1 = new Bar() { Clients = new List<Client>() };
var bar2 = new Bar() { Clients = new List<Client>() };
bar1.Clients.Add(new Client());
bar1.Clients.Add(new Client());
bar2.Clients.Add(new Client());
bar2.Clients.Add(new Client());
context.Bars.AddRange(new[] { bar1, bar2 });
await context.SaveChangesAsync();
bar1.Clients.Add(new Client());
bar2.Clients.Add(new Client());
bar1.Clients.Add(new Client());
await context.SaveChangesAsync();
将呈现
ClientId BarId ClientNumber
1 1 1
2 1 2
6 1 3
7 1 4
3 2 1
4 2 2
5 2 3
缺点是您不能使用唯一索引 IX_CLIENT_NUMBER,因为在触发器执行之前 ClientNumber 将为 NULL。
我用 AutoMapper IValueResolver 解决了这个问题...
public class PublishedEntityVersionResolver : IValueResolver<CreatePublishedEntityCommand, PublishedEntity, int>
{
private readonly IApplicationDbContext _dbContext;
public PublishedEntityVersionResolver(IApplicationDbContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public int Resolve(
CreatePublishedEntityCommand source, PublishedEntity destination, int destinationMember, ResolutionContext context)
{
// If this EntityId & TenantId has been previously published, get it's version and increment otherwise start afresh...
var previousVersion = _dbContext.PublishedEntities
.SingleOrDefault(entity => entity.EntityId == source.EntityId && entity.TenantId == source.TenantId);
return previousVersion?.Version + 1 ?? 1;
}
}
public void Mapping(Profile profile)
=> profile.CreateMap<CreatePublishedEntityCommand, PublishedEntity>()
.ForMember(
entity => entity.Version,
options => options.MapFrom<PublishedEntityVersionResolver>());
我正在寻找最佳实践,以便使用 EF Core 在我的数据库中增加一些数字..
我有一个名为 PARENT 的数据库模型。在我的应用程序中,我可以有几个 PARENTS。每个 PARENT 有几个孩子。
PARENT1:孩子 1 - 孩子 2
PARENT2:孩子 1 - 孩子 2 - 孩子 3
我希望在每个 PARENT 中,每个 children 都按一列递增编号:int Number
。这意味着,
PARENT1.CHILD1 = 001;
PARENT1.CHILD2 = 002;
PARENT2.CHILD1 = 001;
PARENT2.CHILD2 = 002;
...
所以,它只能是唯一的,并且递增 parent..
任何人都可以给我一些最佳实践以有效地实现这一目标吗?
更新:
public class Bar {
public Guid BarId {get;set;}
public ICollection<Client> Clients {get;set;}
}
public class Client {
public Guid ClientId {get;set;}
public Guid BarId {get;set;}
public int ClientNumber {get;set;}
}
public class Context : DBContext {
entity.HasKey(e => new { e.BarId, e.ClientId }).HasName("PK_CLIENT");
entity.Property(e => e.ClientId).ValueGeneratedWhenDefault();
entity.Property(e => e.ClientNumber).ValueGeneratedOnAdd();
entity.HasIndex(e => new {e.BarId, e.ClientNumber}).IsUnique()
.HasName("IX_CLIENT_NUMBER");
}
通过调用 ValueGeneratedOnAdd()
我使数字列递增,但我希望它为每个 Bar 递增,因此它应该允许不同 bar 之间重复的客户编号,但不能在同一个 Bar 上,并且也递增他们由 Bar,而不仅仅是客户实体。
也许这不是你想要的,但由于我正在编写一些代码,所以它不适合作为评论。
要在子组合中设置 ClientNumber,我会编写如下代码:
Bar bar = new Bar();
int seq = 1;
bar.Clients.Add(new Client() { ClientNumber = seq++; });
bar.Clients.Add(new Client() { ClientNumber = seq++; });
dbContext.Update(bar);
dbContext.SaveChanges();
创建新栏时它工作正常。如果它可以被编辑并且客户端可以被添加或从栏中删除 - 就像一个 CRUD - 那么你必须改变一些东西。但它显示了我在评论中谈论的内容(设置 ClientNumber)。
另一种方法是拦截 SaveChanges,在这种情况下我会避免这样做,因为对于不明智的开发人员来说这有点不清楚:
public class DbContextWithBlackMage : DbContext
{
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
OnBeforeSaving();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
private void OnBeforeSaving()
{
var entries = ChangeTracker.Entries();
foreach (var entry in entries)
{
//TODO black mage to fill the ClientNumber very secretly and obscurely
//TODO Matheus will be angry when he finds out muahahahahaha!
}
}
}
无论如何...这可能不是您想要的,但也许它会激发人们对如何解决(或如何不解决)的意愿。
一种方法是执行以下操作;
1) 使用整数作为键,并设置 ClientNumber 属性 可以为空。
public class Bar
{
public int BarId { get; set; }
public ICollection<Client> Clients { get; set; }
}
public class Client
{
public int ClientId { get; set; }
public Bar Bar { get; set; }
public int BarId { get; set; }
public int? ClientNumber { get; set; }
}
2) 设置ClientId为identity(自动递增),ClientNumber为数据库生成的(否则EF Core插入后不会从数据库中重新读取值)
entity.Property(e => e.ClientId).ValueGeneratedOnAdd();
entity.Property(e => e.ClientNumber).ValueGeneratedOnAddOrUpdate();
3) 插入后添加修改ClientNumber的触发器
create trigger ClientNumber on Clients after insert
as
begin
set nocount on;
update c set ClientNumber = n.RowNum from Clients c
inner join (
select ClientId, ROW_NUMBER() OVER(PARTITION BY BarId ORDER BY ClientId ASC) as RowNum
from Clients
) n on n.ClientId = c.ClientId
where c.ClientId in (select ClientId from inserted)
end
现在一切都自动插入:
var bar1 = new Bar() { Clients = new List<Client>() };
var bar2 = new Bar() { Clients = new List<Client>() };
bar1.Clients.Add(new Client());
bar1.Clients.Add(new Client());
bar2.Clients.Add(new Client());
bar2.Clients.Add(new Client());
context.Bars.AddRange(new[] { bar1, bar2 });
await context.SaveChangesAsync();
bar1.Clients.Add(new Client());
bar2.Clients.Add(new Client());
bar1.Clients.Add(new Client());
await context.SaveChangesAsync();
将呈现
ClientId BarId ClientNumber
1 1 1
2 1 2
6 1 3
7 1 4
3 2 1
4 2 2
5 2 3
缺点是您不能使用唯一索引 IX_CLIENT_NUMBER,因为在触发器执行之前 ClientNumber 将为 NULL。
我用 AutoMapper IValueResolver 解决了这个问题...
public class PublishedEntityVersionResolver : IValueResolver<CreatePublishedEntityCommand, PublishedEntity, int>
{
private readonly IApplicationDbContext _dbContext;
public PublishedEntityVersionResolver(IApplicationDbContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public int Resolve(
CreatePublishedEntityCommand source, PublishedEntity destination, int destinationMember, ResolutionContext context)
{
// If this EntityId & TenantId has been previously published, get it's version and increment otherwise start afresh...
var previousVersion = _dbContext.PublishedEntities
.SingleOrDefault(entity => entity.EntityId == source.EntityId && entity.TenantId == source.TenantId);
return previousVersion?.Version + 1 ?? 1;
}
}
public void Mapping(Profile profile)
=> profile.CreateMap<CreatePublishedEntityCommand, PublishedEntity>()
.ForMember(
entity => entity.Version,
options => options.MapFrom<PublishedEntityVersionResolver>());