C#实体框架代码首先创建控制器

时间:2016-07-18 07:21:15

标签: c# entity-framework

我想学习实体框架代码,但我无法解决问题。 当我想创建一个控制器(使用AnimeDbContext形成Model类Anime) 我有错误Image Of Error

我看到外键有些问题,但我不知道为什么。 这是Anime类的代码

namespace MyBD.Models
{
    [Table("Anime")]

    public class Anime
    {


        [Key] // ustawiamy klucz główny tabeli
        public int Id { get; private set; }
        [Required]
        public string Title { get; private set; }
        public string Description { get; private set; }

        public Anime(int id=0, string title="New", string description="Description") 
        {
            this.Id = id;
            this.Title = title;
            this.Description = description;
        }



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


    }

}

和AnimeComment课程

    namespace MyBD.Models
{
    [Table("CommentAnime")]
    public class CommentAnime
    {
        [Key]
        public int Id { get; private set; }
        [ForeignKey("anime")] // ustawiamy klucz obcy
        [Required]
        public int AnimeId { get; set; }
        public string Opinion { get; private set; }
        public int Mark { get; private set; }

        public CommentAnime(int id=0, string opinion="My Opinion", int mark=0)
        {
            this.Id = id;
            this.Opinion = opinion;
            this.Mark = mark;
        }

        public virtual Anime anime { get; set; }

2 个答案:

答案 0 :(得分:1)

Phillip的答案已经涵盖了模型中所需的大部分更改。我只想更多地了解它,并为您提供如何使用FluentAPI配置dbContext类。这样,您可以跳过类上的Data Annotation属性:

动漫类:

public class Anime
{
    public int Id { get; set; }
    public string Title { get; set;}
    public string Description { get; set; }

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

public class CommentAnime
{

    public int Id { get; set; }
    public string Opinion { get; set; }
    public int Mark { get; set; }

    public int AnimeId { get; set; }
    public virtual Anime anime { get; set; }
}

AnimeDbContext类

//Program Table
//Title Field
modelBuilder.Entity<Anime>()
            .Property(t => t.Title)
            .HasColumnType("varchar")
            .HasMaxLength(120) //define some Length..
            .IsRequired(); //Will be translated as Not Null


//CommentAnime
//Opinion Field
modelBuilder.Entity<CommentAnime>()
            .Property(t => t.Opinion)
            .HasColumnType("varchar")
            .HasMaxLength(120) //define some Length..
            .IsRequired(); //Will be translated as Not Null


//Configuring the One-to-Many Relationship

modelBuilder.Entity<CommentAnime>()
            .HasRequired(t => t.Anime)
            .WithMany(x => x.Comments)
            .HasForeignKey(t => t.AnimeId);

如果您想在CommentAnime中拥有可以为空的ForeinKey,则可以使用HasOptional代替HasRequired。请注意,您也不需要为您的Id列创建配置,因为EF使用naming conventions

这样您的POCO类就干净且独立于任何EF配置。所有数据库建模都集中在AnimeDbContext类中。我个人认为这是最好的方法,因为你避免使用属性来规避你的类,但是,我想这只是个人的事情。

答案 1 :(得分:0)

在实体框架中,我发现如果您班级中的任何财产被称为&#34; Id&#34;,则以大写或小写,&#34; ID&#34;或&#34; id&#34;,它将引用它作为主键。因此,您不需要[Key]数据注释。

您也不需要[Table()]数据注释,因为EF将默认为表名的类名。

您也不需要输入公共构造函数,因为您可以使用视图来设置占位符文本。您可以使用默认构造函数作为默认值,例如设置创建日期,例如参见Anime类。

请参阅下面的新课程。

public class Anime
{
    public Anime()
    {
        Description = "This is a desciption";
        DateCreated= DateTime.UtcNow;

    }

    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime DateCreated { get; set; }      


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


}

通过将父表引用为虚拟并使用&#34; nameId&#34;来在EF中进行连接时EF再次为您自动解决问题。

public class CommentsAnime
{
    public int Id { get; set; }
    public int AnimeId { get; set; } //Using the virtual table name
    public string Opinion { get; private set; }
    public int Mark { get; private set; }

    public virtual Anime Anime { get; set; }

}