Asp.net mvc 连接字符串的模型将被覆盖
这里是MVC的新手,所以为可能是个愚蠢的问题道歉 我已经为现有数据库创建了一些database first实体框架模型,这非常有效。其中一个模型从数据库中提取名字和姓氏,我可以在代码中引用它们Asp.net mvc 连接字符串的模型将被覆盖,asp.net-mvc,entity-framework,entity-framework-6,Asp.net Mvc,Entity Framework,Entity Framework 6,这里是MVC的新手,所以为可能是个愚蠢的问题道歉 我已经为现有数据库创建了一些database first实体框架模型,这非常有效。其中一个模型从数据库中提取名字和姓氏,我可以在代码中引用它们 namespace manage.mysite.com.DataModels { using System; using System.Collections.Generic; public partial class UserProfile { [Sys
namespace manage.mysite.com.DataModels
{
using System;
using System.Collections.Generic;
public partial class UserProfile
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public UserProfile()
{
this.webpages_Roles = new HashSet<webpages_Roles>();
this.Property_Info = new HashSet<Property_Info>();
}
public string Email { get; set; }
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string TelNumber { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Property_Info> Property_Info { get; set; }
}
}
这工作绝对完美,除了每次我需要从数据库更新模型时,这显然会被覆盖的问题。如何阻止这种情况发生?使用属性:
[NotMapped]
public string FullName {
get
{
return FirstName + " " + LastName;
}
}
那就是使用。如果您愿意,可以这样做:
modelBuilder.Entity<UserProfile>().Ignore(t => t.FullName);
这是完美的工作。我需要参考System.ComponentModel.DataAnnotations.Schema,现在一切都好了。谢天谢地,我错了,当我从数据库刷新模型时,它仍然被删除。有什么想法吗?我应该把它移到一个单独的模型吗?现在我注意到你正在使用一个现有的数据库,是的,在这种情况下,使用一个分部类在同一个模型中添加像这样的自定义属性是的?是的,在你有实体类的同一个项目中添加分部类。
modelBuilder.Entity<UserProfile>().Ignore(t => t.FullName);
public partial class UserProfile
{
[NotMapped]
public string FullName {
get
{
return FirstName + " " + LastName;
}
}
}