Entity Framework 4 Code First 是否支持 NHibernate 等身份生成器?

发布于 2024-10-21 22:14:46 字数 440 浏览 1 评论 0原文

一年前提出的这个问题是相似的: Entity Framework 4 是否支持以下生成器id 值像 NHibernate?

但我想知道的是代码优先 CTP 是否添加了对身份生成策略的支持。如果没有,有人知道 EF 中有一个很好的扩展点来实现类似的功能吗?

我目前正在使用使用 GUID 作为标识符的模型类。使用 EF 插入时,它们保留其 Guid.Empty 初始值。我知道您可以将数据库中的列的默认值设置为 newid() ,但这违背了客户端身份生成的目的。

实体框架是否还不够成熟,无法在分布式、断开连接的系统中使用?

This question, asked a year ago, is similar:
Does the Entity Framework 4 support generators for id values like NHibernate?

But what I'd like to know is if the code first CTP adds support for identity generation strategies. If not, does anyone know a good extension point in EF to implement something similar?

I'm currently working with model classes which use GUID as the identifier. When inserting using EF they retain their Guid.Empty initial values. I know that you can set a default value for the column in the DB to newid() but that defeats the purpose of client-side identity generation.

Is Entity Framework just not mature enough to be used in a distributed, disconnected system?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东走西顾 2024-10-28 22:14:46

不,实体框架代码优先仍然只是 EFv4 的良好包装。没有类似 NHibernate 的生成器。如果您需要客户端 Id 生成器,则必须重写派生的 DbContext 中的 SaveChanges 并实现您自己的将 Id 分配给新实体的逻辑。

编辑:

一些高级示例:

public class Context : DbContext
{
    // Helper for example
    // DO NOT USE IN REAL SCENARIOS!!!
    private static int i = 0; 

    public DbSet<MyEntity> MyEntities { get; private set; }

    public Context()
        : base("connection")
    {
        MyEntities = Set<MyEntity>();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<MyEntity>().HasKey(e => e.Id);
        // Turn off autogeneration in database
        modelBuilder.Entity<MyEntity>()
                    .Property(e => e.Id)
                    .HasDatabaseGeneratedOption(HasDatabaseGeneratedOption.None);

        // Other mapping
    }

    public override int SaveChanges()
    {
        foreach (var entry in ChangeTracker.Entries<MyEntity>()
            .Where(e => e.State == EntityState.Added))
        {
            // Here you have to add some logic to generate Id
            // I'm using just static field
            entry.Entity.Id = ++i;  
        }

        return base.SaveChanges();
    }
}

public class MyEntity
{
    public int Id { get; set; }
    // Other properties
}

No, Entity framework code-first is still just nice wrapper around EFv4. There are no NHibernate like generators. If you want client side Id generator you will have to override SaveChanges in derived DbContext and implement your own logic of assigning Ids to new entities.

Edit:

Some high level example:

public class Context : DbContext
{
    // Helper for example
    // DO NOT USE IN REAL SCENARIOS!!!
    private static int i = 0; 

    public DbSet<MyEntity> MyEntities { get; private set; }

    public Context()
        : base("connection")
    {
        MyEntities = Set<MyEntity>();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<MyEntity>().HasKey(e => e.Id);
        // Turn off autogeneration in database
        modelBuilder.Entity<MyEntity>()
                    .Property(e => e.Id)
                    .HasDatabaseGeneratedOption(HasDatabaseGeneratedOption.None);

        // Other mapping
    }

    public override int SaveChanges()
    {
        foreach (var entry in ChangeTracker.Entries<MyEntity>()
            .Where(e => e.State == EntityState.Added))
        {
            // Here you have to add some logic to generate Id
            // I'm using just static field
            entry.Entity.Id = ++i;  
        }

        return base.SaveChanges();
    }
}

public class MyEntity
{
    public int Id { get; set; }
    // Other properties
}
似狗非友 2024-10-28 22:14:46

不。

我的实体框架4.1.10715由NuGet安装。
也许您可以使用属性,

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get;set;}

请参阅(EF 4.1中支持的注释的完整列表:页面中的CTRL+F)此处

No.

Mine Entity Framework 4.1.10715 installed by NuGet.
maybe you could use attribute

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get;set;}

please see (The full list of annotations supported in EF 4.1 : CTRL+F in page) here.

虐人心 2024-10-28 22:14:46
[Key]
public string Id { get; set; }

然后使用新的 GUID ToString

Units.Add( new Unit(){Id=Guid.NewGuid().ToString(), Name="123"});
[Key]
public string Id { get; set; }

and then use new GUID ToString

Units.Add( new Unit(){Id=Guid.NewGuid().ToString(), Name="123"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文