实体框架 - 确定给定类型的HasDatabaseGeneratedOption设置 [英] Entity Framework - Determine the HasDatabaseGeneratedOption setting for a given type

查看:1749
本文介绍了实体框架 - 确定给定类型的HasDatabaseGeneratedOption设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的DB中的某些属性,我需要手动计算它们的Id#,所以对于这些属性,我执行 .Property(p => p.Id).HasDatabaseGeneratedOption(System.ComponentModel。 DataAnnotations.Schema.DatabaseGeneratedOption.None); 里面 OnModelCreating

For some of the properties in my DB I need to manually calculate their Id #, so for those properties I do .Property(p => p.Id).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); inside OnModelCreating

然后,在存储库我有一种计算给定类型的Id的方法。我希望让系统变得聪明,并检查一下$ code DatabaseGeneratedOption.None 或 DatabaseGeneratedOption.Identity 和返回下一个Id或0。

Then, in the repository I have a method to calculate out the Id for a given type. I would prefer to have the system be intelligent and check to see if the DatabaseGeneratedOption.None or DatabaseGeneratedOption.Identity and either return the next Id or 0.

如何检查(从存储库内)什么是$ code DatabaseGeneratedOption 给定类型(T)是?

How can I check (from inside a repository) what the DatabaseGeneratedOption for a given type (T) is?

推荐答案

正如我在评论中所说,您的解决方案凭借代码优先商店从 EntityType 返回CLR类型名称的模型。然而,数据库优先存储模型返回商店名称。这些名称不一定与CLR类型名称相匹配。

As I said in a comment, your solution works by virtue of a code-first store model returning the CLR type names from EntityType. A database-first store model, however, returns the store names. These names do not necessarily match the CLR type names.

为了使此方法独立于EF的存储模型,我们需要访问存储CLR映射空间( CSSpace ),并找到 EntitySet (由CLR名称),并匹配其 KeyMembers 与商店模型中的列( Property.Column ),因为这些列包含正确的值 IsStoreGeneratedIdentity 。 (CLR属性也有这个属性,但它总是假的)。

To make this method independent of EF's store model, we need to access the store-CLR mapping space (CSSpace) and find the EntitySet (by CLR name) and match its KeyMembers with the columns in the store model (Property.Column), because these columns contain the right value of IsStoreGeneratedIdentity. (CLR properties also have this property, but it's always false).

所以这就是我们得到的(作为封装在一个 DbContext subtype):

So this is what we get (as a method encapsulated in a DbContext subtype):

public bool IsStoreGeneratedIdentity<TEntity>()
{
    var entityContainerMappings = (this as IObjectContextAdapter).ObjectContext
        .MetadataWorkspace.GetItems(DataSpace.CSSpace)
        .OfType<EntityContainerMapping>();

    var entityTypeMappings = entityContainerMappings
        .SelectMany(m => m.EntitySetMappings
            .Where(esm => esm.EntitySet.ElementType.Name == typeof(TEntity).Name))
        .SelectMany(esm => esm.EntityTypeMappings).ToArray();

    var keyMappings = (from km in entityTypeMappings.SelectMany(etm => etm.EntityType.KeyMembers)
        join pm in entityTypeMappings.SelectMany(etm => etm.Fragments)
            .SelectMany(mf => mf.PropertyMappings)
            .OfType<ScalarPropertyMapping>()
            on km.Name equals pm.Property.Name
        select pm
        );

    return keyMappings.Any(pm => pm.Column.IsStoreGeneratedIdentity);
}

这篇关于实体框架 - 确定给定类型的HasDatabaseGeneratedOption设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆