在ASP.Net Core中使用[Column]或[ColumnAttribute] DataAnnotation的最佳方法

时间:2017-10-13 06:27:19

标签: sql-server asp.net-core data-annotations

我是asp.net核心MVC框架的新手(在此之前我使用的是Laravel。)

我有两个表,Country和State,我将使用InnerJoin加入这两个表。

在我的CountryModel中,我有4个属性,如下面的

public int CountryId { get; set; }

public string CountryName { get; set; }

public int StateId { get; set; }

public string StateList { get; set; }



SELECT c.CountryId,
       c.CountryName,
       s.StateId,
       s.States AS 'StateName'
       FROM _tbAddr_Country AS c
       INNER JOIN _tbAddr_State AS s on s.CountryId = c.CountryId

以上是我的sql查询,在我的国家模型属性中我没有“StateName”属性但我有“StateList”,我只想通过使用DataAnnotation将“StateList”映射到“StateName”,就像下面一样,我有试过

[ColumnAttribute("StateName")]
public string StateList { get; set; }

[Column("StateName")]
public string StateList { get; set; }

这两个对我不起作用,我知道我可以通过简单地将“StateList”更改为“StateName”来解决这个问题,但我希望我能找到使用“Column”或“ColumnAttribute”数据注释的替代方法解决这个问题。任何帮助表示赞赏。

P / S:我使用.net核心1.1和SQL Server Management Studio 2014

1 个答案:

答案 0 :(得分:0)

这不符合您的想法。

列必须与表列名匹配。但是,您有一个使用连接的投影。所以没有这样一个名字的表。

EntityFramework Core 1.x和2.0不支持从视图或查询到任意模型的ad-hoc映射。此功能目前位于roadmap for 2.1作为" Read-only view types in the model"。

请注意,此功能位于"伸展目标"部分。这意味着,它的优先级为2.1,但是变化是并非所有目标都能达到2.1版本,并且如果在2.1发布时没有完成,可能会被推到下一个版本(这应该是ASP.NET Core 2.1和/或.NET Core 2.1发布时的时间