在 ASP.Net 核心中使用 [Column] 或 [ColumnAttribute] DataAnnotation 的最佳方法
Best Approach to use [Column] or [ColumnAttribute] DataAnnotation in ASP.Net Core
我是 asp.net 核心 MVC 框架的新手(在此之前我使用的是 Laravel。)
我有两个 table,国家和州,我将使用 InnerJoin 加入这两个 table。
在我的 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 core 1.1 和 SQL Server Management Studio 2014
这不是你想的那样。
列必须匹配 table 列名称。但是,您有一个使用连接的投影。所以没有table这样的名字。
EntityFramework Core 1.x 和 2.0 不支持从视图或查询到任意模型的临时映射。此功能目前在 roadmap for 2.1 as "Read-only view types in the model"。
请注意,此功能位于 "stretch goal" 部分。这意味着,它优先用于 2.1,但变化是并非所有目标都会进入 2.1 版本,如果在 2.1 发布时还没有完成,可能会被推到下一个版本(应该是 ASP.NET Core 2.1 and/or .NET Core 2.1 版本)
我是 asp.net 核心 MVC 框架的新手(在此之前我使用的是 Laravel。)
我有两个 table,国家和州,我将使用 InnerJoin 加入这两个 table。
在我的 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 core 1.1 和 SQL Server Management Studio 2014
这不是你想的那样。
列必须匹配 table 列名称。但是,您有一个使用连接的投影。所以没有table这样的名字。
EntityFramework Core 1.x 和 2.0 不支持从视图或查询到任意模型的临时映射。此功能目前在 roadmap for 2.1 as "Read-only view types in the model"。
请注意,此功能位于 "stretch goal" 部分。这意味着,它优先用于 2.1,但变化是并非所有目标都会进入 2.1 版本,如果在 2.1 发布时还没有完成,可能会被推到下一个版本(应该是 ASP.NET Core 2.1 and/or .NET Core 2.1 版本)