gpt4 book ai didi

c# - Entity Framework 中首先从数据库更新模型的元数据不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:26 25 4
gpt4 key购买 nike

我想为模型添加更多验证消息注释(这里的所有模型都是首先由数据库生成的),所以我确实通过此链接的帮助使用了元数据https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation .没有更新任何模型,但构建失败并显示警告:

Error 6046: Unable to generate function import return type of the store function 'fn_diagramobjects'.

Error CS1061 'Account' does not contain a definition for 'ConfirmPassword' and no extension method 'ConfirmPassword' accepting a first argument of type 'Account' could be found (are you missing a using directive or an assembly reference?) project-asp-mvc D:.NET demo\Project\project-asp-mvc\Controllers\UserController.cs 85 Active

这是我从数据库生成的模型:

using System;
using System.Collections.Generic;

public partial class Account
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Account()
{
this.Orders = new HashSet<Order>();
}

public string username { get; set; }
public string password { get; set; }
public Nullable<int> role_id { get; set; }

public virtual Role Role { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Order> Orders { get; set; }
}

元数据.cs:

using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace project_asp_mvc.Models
{
public class AccountMetadata
{
[Required]
[StringLength(50, ErrorMessage = "Username con not be longer than 50")]
[EmailAddress]
public string username { get; set; }

[Required]
[DataType(DataType.Password)]
public string password { get; set; }

[Required]
[NotMapped]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Please confirm your password again")]
public string ConfirmPassword { get; set; }
}
}

PartialClasses.cs

 using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace project_asp_mvc.Models
{
[MetadataType(typeof(AccountMetadata))]
public partial class Account
{
}
}

最佳答案

AccountMetadata 类中的这些代码行来自何处​​。

[Required]
[NotMapped]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Please confirm your password again")]
public string ConfirmPassword { get; set; }

您似乎已向帐户类中不存在的属性添加了注释。我建议您删除这些行并重试。

关于c# - Entity Framework 中首先从数据库更新模型的元数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46460820/

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