简体   繁体   English

实体框架列映射

[英]Entity Framework Column Mapping

I have an issue with mapping an object property to a column from a database function. 将对象属性映射到数据库函数中的列时遇到问题。

The database function returns a column called [On Hand] . 数据库函数返回称为[On Hand]的列。 Therefore my model property is called OnHand . 因此,我的模型属性称为OnHand

This obviously does not map correctly and fails to retrieve the data correctly for that column. 这显然不能正确映射,并且无法正确检索该列的数据。

I have attempted the following in order to resolve this: 为了解决这个问题,我尝试了以下操作:

Editing the model to use an annotation 编辑模型以使用注释

[Column("On Hand")]
public int OnHand { get; set; }

Using Fluent API 使用Fluent API

modelBuilder.Entity<BinDetail>()
    .Property(e => e.OnHand)
    .HasColumnName("On Hand");

Neither of these approaches have worked either together or independently. 这些方法都没有一起或独立地起作用。

The only way i can get this to work on the test database is to alter the return column of the function to [OnHand] , however, due to other systems using this function, this is not an option to use on the live database. 我可以在测试数据库上使用此方法的唯一方法是将函数的返回列更改为[OnHand] ,但是,由于其他系统正在使用此函数,因此不能在实时数据库上使用此选项。

Any suggestions anybody has would be greatly appreciated 任何人的任何建议将不胜感激

After digging into my code, i have realised that the way i have executed the function is by using: 深入研究我的代码后,我意识到执行函数的方式是使用:

Database.SqlQuery<BinDetail>("Query for function").ToList();

Therefore, I realised that one solution for this would be to alter the query from: 因此,我意识到一种解决方案是将查询更改为:

SELECT * FROM.....

to: 至:

SELECT ......, [On Hand] AS OnHand..... . SELECT ......, [On Hand] AS OnHand.....

This does work and seems to retrieve data correctly, however, i dont think it is very pretty or good practice. 这确实有效,并且似乎可以正确检索数据,但是,我认为这不是非常好的方法。

Therefore, if anybody has a more elegant solution to this or for calling a function, then please let me know as i am always looking to improve my code and our standards. 因此,如果有人对此有更优雅的解决方案或可以调用函数,请让我知道,因为我一直在寻求改进代码和标准的方法。

If you're using Entity Framework Core 1.0 RC 1, there is a bug (it's fixed at RC2 and onwards) causes this. 如果您使用的是Entity Framework Core 1.0 RC 1,则存在一个错误(已在RC2及更高版本中修复)。

A workaround is ordering fields by A to Z, a quick sample: 一种解决方法是按A到Z的顺序对字段进行排序,这是一个快速示例:

"SELECT " + GetColumnNames<Unit>("R") + " FROM Unit AS R"

Helper methods: 辅助方法:

private static Dictionary<Type, PropertyInfo[]> getPropertiesCache = new Dictionary<Type, PropertyInfo[]>();

public static string GetColumnNames<T>(string prefix)
{
    var columns = GetProperties(typeof(T)).OrderBy(i => i.Name).Select(i => $"[{prefix}].[{i.Name}]");

    return string.Join(", ", columns);
}

public static IEnumerable<PropertyInfo> GetProperties(Type type)
{
    if (getPropertiesCache.ContainsKey(type))
        return getPropertiesCache[type].AsEnumerable();

    var properties = type
        .GetTypeInfo()
        .DeclaredProperties;

    getPropertiesCache.Add(type, properties.ToArray());

    return getPropertiesCache[type].AsEnumerable();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM