簡體   English   中英

C#實體框架代碼首次創建控制器

[英]C# Entity Framework Code First-create controller

我想先學習實體框架代碼,但不能解決問題。 當我想創建控制器(使用AnimeDbContext的Model類動漫)時,出現錯誤Image Of Error

我看到外鍵存在一些問題,但我不知道為什么。 這是動漫類的代碼

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


    }

}

和類動漫評論

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

Phillip的答案已經涵蓋了您模型中所需的大多數更改。 我只想在此方面做更多的工作,並向您提供如何使用FluentAPI配置dbContext類的方法。 這樣,您可以跳過類上的“數據注釋”屬性:

動漫類:

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 請注意,由於EF使用命名約定 ,因此您也不需要為Id列創建配置。

這樣,您的POCO類是干凈的,並且與任何EF配置無關。 所有數據庫建模都集中在AnimeDbContext類中。 我個人認為這是最好的方法,因為您避免使用屬性來污染類,但是,我想這只是個人的事情。

在實體框架中,我發現如果類中的任何屬性稱為“ Id”,則以大寫或小寫形式稱為“ ID”或“ id”,它將把它稱為主鍵。 因此,您不需要[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; }


}

通過將父表引用為虛擬表並使用“ nameId”在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; }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM