c# - 避免将属性映射到表

标签 c# .net entity-framework ef-code-first

谁能告诉我如何避免使用代码优先方法将某些属性从我的模型映射到表?

例子:

public class Post
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "Posted")]
    public DateTime Created { get; set; }

    [Display(Name = "Modified")]
    public DateTime? Modified { get; set; }

    [Required]
    [MaxLength(256)]
    public string Title { get; set; }

    [Required]
    public string Body { get; set; }

    public int? UserId { get; set; }

    public string UserName { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }

    public virtual ICollection<Tag> Tags { get; set; } 
}

我希望用户名不映射到表中。在 EF 中是否可能?

最佳答案

添加一个

[NotMapped] 

属性到所需的属性:

public class Post
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "Posted")]
    public DateTime Created { get; set; }

    [Display(Name = "Modified")]
    public DateTime? Modified { get; set; }

    [Required]
    [MaxLength(256)]
    public string Title { get; set; }

    [Required]
    public string Body { get; set; }

    public int? UserId { get; set; }

    [NotMapped]
    public string UserName { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }

    public virtual ICollection<Tag> Tags { get; set; } 
}

关于c# - 避免将属性映射到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12337453/

相关文章:

c# - 如何判断 Stream 何时结束?

c# - 如何在标准 WPF ListView 中启用 UI 虚拟化

javascript - 正则表达式从具有多个方括号的字符串中查找匹配项

c# - 用集合EntityFramework连接表

c# - 在 Entity Framework 中使用TPT设计的多级继承

c# - 错误时显示标签 (HTML CSS)

c# - 如何在 MVC Razor DropDownList 中使用 Html.Raw?

c++ - .net 中的私有(private)成员?

c# - 用c#压缩avi视频

c# - 存储过程无法在 Entity Framework 中创建复杂类型