- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我不想手动将每个映射类添加到 ModelBuilder(),因此尝试使用我有限的反射知识来注册它们。这就是我所拥有的,这是我得到的错误:
代码:
private static ModelBuilder CreateBuilder() {
var contextBuilder = new ModelBuilder();
IEnumerable<Type> configurationTypes = typeof(DatabaseFactory)
.Assembly
.GetTypes()
.Where(type => type.IsPublic && type.IsClass && !type.IsAbstract && !type.IsGenericType && typeof(EntityTypeConfiguration).IsAssignableFrom(type) && (type.GetConstructor(Type.EmptyTypes) != null));
foreach (var configuration in configurationTypes.Select(type => (EntityTypeConfiguration)Activator.CreateInstance(type)))
{
contextBuilder.Configurations.Add(configuration);
}
return contextBuilder;
}
错误:错误 2 无法从用法中推断方法“System.Data.Entity.ModelConfiguration.Configuration.ConfigurationRegistrar.Add(System.Data.Entity.ModelConfiguration.EntityTypeConfiguration)”的类型参数。尝试明确指定类型参数。 C:\root\development\playground\PostHopeProject\PostHope.Infrastructure.DataAccess\DatabaseFactory.cs 67 17 PostHope.Infrastructure.DataAccess
最佳答案
原答案:
隐含解决方案的详细信息:
上面引用的文章表明,您可以使用 dynamic
关键字绕过编译时类型检查,从而绕过尝试将配置添加到通用 Add()< 的限制
DbModelBuilder
的方法。这是一个快速示例:
// Load all EntityTypeConfiguration<T> from current assembly and add to configurations
var mapTypes = from t in typeof(LngDbContext).Assembly.GetTypes()
where t.BaseType != null && t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)
select t;
foreach (var mapType in mapTypes)
{
// note: "dynamic" is a nifty piece of work which bypasses compile time type checking... (urgh??)
// Check out: http://msdn.microsoft.com/en-us/library/vstudio/dd264741%28v=vs.100%29.aspx
dynamic mapInstance = Activator.CreateInstance(mapType);
modelBuilder.Configurations.Add(mapInstance);
}
您可以阅读有关使用此关键字的更多信息 on MSDN
关于c# - 为 Entity Framework CPT5 构建 EntityTypeConfiguration 列表的反射(reflection),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4383024/
如何自动添加当前程序集中的所有 EntityTypeConfiguration<>? public class Entities : DbContext { public Entities()
背景: 我公司目前的结构是使用 Plinqo/Linq to Sql 创建“数据访问对象”,然后使用一组自定义的 CodeSmith 模板来构建“业务对象”。长话短说,这两组对象非常紧密耦合,并且使用
我刚开始制作 EntityTypeConfiguration 类并做了以下操作 public class Xyz { public int PlaceId { get; set; } p
我使用 Entity Framework Code First 和从 EntityTypeConfiguration 继承的映射类。我这样做是为了封装我使用 Code First fluent API
获取 Base 对象属性的集中映射是否有一些技巧?使用 EntityTypeConfiguration 时是否有一些抽象类的简单模式。 非常感谢任何提示。我无法声明一个类 Public class B
在EF6中我们通常可以使用这种方式来配置Entity。 public class AccountMap : EntityTypeConfiguration { public AccountMa
我有一个数据库结构,其中有一个设备表,其中包含列 Equipment_Id、Field_1 和 Field_2。我有一个 Equipment_Locale 表,其中包含字段 Equipment_Id
代码 我的应用程序中有两个非常简单的界面 表示保存在数据库中的实体 public interface IEntity { int Id { get; set; } } 只有Nome字段作为保存
我有一个“主”Visual Studio 项目,其中包含类似于以下内容的 Entity Framework 映射: public class UserMap : EntityTypeConfigura
使用 DataAnnotations 设置属性很简单: [RegularExpression("^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]
我一直在使用带有每个实体专用映射类的EF Code First CTP5,如下所示: public class UserMapping : EntityTypeConfiguration {
我正在尝试编写 T4 模板来为我的模型类生成自定义 View ,模型类位于另一个程序集中,映射基于 EF6 fluent API。 目前,我正在使用反射来获取属性的名称和类型,我的问题是如何读取 Mo
我正在为音乐轨道和专辑建模,其中专辑有很多轨道,轨道只能在一张专辑中,并使用连接表指定它在专辑列表中的位置。 这是我的模型: public class Track { public int I
我不想手动将每个映射类添加到 ModelBuilder(),因此尝试使用我有限的反射知识来注册它们。这就是我所拥有的,这是我得到的错误: 代码: private static ModelBuilder
我正在尝试使用一个通用的 EntityTypeConfiguration 类来为我的所有实体配置主键,以便每个派生配置类不会重复自身。我的所有实体都实现了一个通用接口(interface) IEnti
我研究EF的时间不长,找不到这个问题的答案。我有现有的数据库,我正在使用 CodeFirst 为模型创建类。 使用Attributes 和EntityTypeConfiguration 定义表列的参数
我是一名优秀的程序员,十分优秀!