gpt4 book ai didi

c# - 部分类中的自定义属性(无效的列名称)

转载 作者:行者123 更新时间:2023-12-02 07:45:17 25 4
gpt4 key购买 nike

我通过脚本创建了数据库,然后使用脚手架命令在 Visual Studio 中创建类,我为实体(用户)添加了一个新的部分类,其中添加了一些自定义属性(这不会​​在数据库上)每当我运行 Get 到此实体时,它都会抛出错误“我在分部类中设置的每个自定义属性的列名称无效”。

异常消息: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; }
}

我添加的部分类。

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);
}
}

上面的任何 Get 方法都会引发错误。

您好!

更多技术细节

EF Core 版本:2.1.0

数据库提供程序:Microsoft.EntityFrameworkCore.SqlServer

操作系统:Windows 10

IDE:Visual Studio Community 2017 15.4.1

最佳答案

将 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 { } }

关于c# - 部分类中的自定义属性(无效的列名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com