C# 实体框架6中的自定义约定
我想创建一个约定,在这个约定中,EF可以将C# 实体框架6中的自定义约定,c#,entity-framework,configuration,entity-framework-6,C#,Entity Framework,Configuration,Entity Framework 6,我想创建一个约定,在这个约定中,EF可以将IEnumerable序列化为一种格式(比如逗号分隔的值),并将它们存储在一列中,然后在取回它们的同时再次创建一个IEnumerable msdn链接提到了以下示例 class DateTimeColumnTypeConvention : IConfigurationConvention<PropertyInfo, DateTimePropertyConfiguration> { public void Apply(
IEnumerable
序列化为一种格式(比如逗号分隔的值),并将它们存储在一列中,然后在取回它们的同时再次创建一个IEnumerable
msdn链接提到了以下示例
class DateTimeColumnTypeConvention : IConfigurationConvention<PropertyInfo, DateTimePropertyConfiguration>
{
public void Apply(
PropertyInfo propertyInfo, Func<DateTimePropertyConfiguration> configuration)
{
// If ColumnType hasn't been configured...
if (configuration().ColumnType == null)
{
configuration().ColumnType = "datetime2";
}
}
}
class DateTimeColumnTypeConvention:IConfigurationConvention
{
公开无效申请(
PropertyInfo PropertyInfo,Func配置)
{
//如果尚未配置ColumnType。。。
if(configuration().ColumnType==null)
{
configuration().ColumnType=“datetime2”;
}
}
}
但我需要像这样的东西
class DateTimeColumnTypeConvention : IConfigurationConvention<PropertyInfo, IEnumerable<string>>
{
public void Apply(PropertyInfo propertyInfo, Func<IEnumerable<string>> configuration)
{
// Get all the values of the property here, create a comma separated string ,ask EF to store this string by setting the ColumnType to varchar and then get it back.
}
}
class DateTimeColumnTypeConvention:IConfigurationConvention
{
公共无效应用(PropertyInfo PropertyInfo,Func配置)
{
//在这里获取属性的所有值,创建一个逗号分隔的字符串,让EF通过将ColumnType设置为varchar来存储该字符串,然后将其取回。
}
}
我找不到任何明显的方法来做这件事。有什么想法吗?为什么不把CRUD作为CSV的责任留给EF,让数据逻辑层负责将其转换为您想要的收集类型?关键不是CRUD,我会在整个数据逻辑层重复相同的代码,如果有多个实体的配置类型是我想通过CustomConvention进行管理的。@JefferyKhan即使作为一个学术练习来测试可以做什么的限制,这应该是一个有趣的问题来解决:)问题是:你能通过映射来做到吗?约定不提供任何额外的映射,它们只提供全局方式(或属性控制方式)来执行fluent映射配置中可能的操作。这就是要点-在映射中不会这样做。如果映射之外没有一些逻辑(在实体中或在上下文中),则当前不支持它。