Jes*_*ios 3 c# visual-studio entity-framework-core asp.net-core
我通过脚本创建了我的数据库,然后使用 scaffold 命令在 Visual Studio 中创建了我的类,我为实体(用户)添加了一个新的部分类,每当我运行时,我在其中添加了一些自定义属性(这不会在数据库中)获取到此实体,它会为我在分部类中设置的每个自定义属性抛出错误无效的列名。
异常消息: SqlException:无效的列名“ResidentialName”。无效的列名“UserDocumentCount”。无效的列名“UserPaymentCount”。
从数据库创建的分部类。
public partial class User
{
public User()
{
UserDocument = new HashSet<UserDocument>();
UserPayment = new HashSet<UserPayment>();
}
public int UserId { get; set; }
public string UserIdentityId { get; set; }
public int? ResidentialId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Street { get; set; }
public string Number { get; set; }
public int Block { get; set; }
public int Lot { get; set; }
public bool Status { get; set; }
public DateTime RegistrationDate { get; set; }
public Residential Residential { get; set; }
public AspNetUsers UserIdentity { get; set; }
public ICollection<UserDocument> UserDocument { get; set; }
public ICollection<UserPayment> UserPayment { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我添加的部分类。
public partial class User
{
public string ResidentialName { get { return this.ResidentialId > 0 && this.Residential != null ? this.Residential.Name : String.Empty; } set { } }
public int UserDocumentCount { get { return this.UserDocument != null ? this.UserDocument.Count : 0; } set { } }
public int UserPaymentCount { get { return this.UserPayment != null ? this.UserPayment.Count : 0; } set { } }
public static async Task<List<User>> GetList(KaiozamaDevContext ctx)
{
return await ctx.User.ToListAsync();
}
public static async Task<User> GetItem(KaiozamaDevContext ctx, int Id)
{
return await ctx.User.SingleOrDefaultAsync(m => m.UserId == Id);
}
public static async Task<User> GetItem(KaiozamaDevContext ctx, string Id)
{
return await ctx.User.SingleOrDefaultAsync(m => m.UserIdentityId == Id);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的任何 Get 方法都会抛出错误。
你好!
更多技术细节
EF 核心版本:2.1.0
数据库提供程序:Microsoft.EntityFrameworkCore.SqlServer
操作系统:Windows 10
IDE:Visual Studio 社区 2017 15.4.1
小智 7
将 NotMapped 属性添加到不在数据库中的属性。
[NotMapped]
public string ResidentialName { get { return this.ResidentialId > 0 && this.Residential != null ? this.Residential.Name : String.Empty; } set { } }
[NotMapped]
public int UserDocumentCount { get { return this.UserDocument != null ? this.UserDocument.Count : 0; } set { } }
[NotMapped]
public int UserPaymentCount { get { return this.UserPayment != null ? this.UserPayment.Count : 0; } set { } }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
855 次 |
| 最近记录: |