gpt4 book ai didi

c# - 使用 Entity Framework 实现标签功能

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

我希望能够以最简单的方式标记任何实体。我有 2 个实体(书籍和作者)

public class Book : AbstractTag
{
[Key]
public int BookId { get; set; }
public string Title { get; set; }
public string Genre { get; set; }

public Author Author { get; set; }
}

public class Author : AbstractTag
{
[Key]
public int AuthorId { get; set; }

public string FirstName { get; set; }
public string LastName { get; set; }

public virtual ICollection<Book> Books { get; set; }
}

它们都继承自AbstractTag

public class AbstractTag : ITaggable
{
public AbstractTag()
{
Tags = new List<Tag>();
}

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

可以标记。我不想要重复的标签。

这是我的标签实体:

public class Tag
{
public Tag()
{
Books = new List<Book>();
Authors = new List<Author>();
}

[Key]
public int TagId { get; set; }

public string Label { get; set; }

public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<Author> Authors { get; set; }
}

public interface ITaggable
{
ICollection<Tag> Tags { get; set; }
}

这是我用来将标签附加到实体的代码:

public interface ITagService
{
TEntity Tag<TEntity>(TEntity entity, string str) where TEntity : ITaggable;
TEntity Untag<TEntity>(TEntity entity, string str) where TEntity : ITaggable;
}

public class TagService : ITagService
{
private readonly Context _context = new Context();

public TagService()
{
}

public TagService(Context context)
{
_context = context;
}

public TEntity Tag<TEntity>(TEntity entity, string str) where TEntity : ITaggable
{
string tag = str.Trim();

if (entity.Tags.All(t => t.Label != tag))
{
Tag t = _context.Tags.Add(new Tag {Label = tag});
entity.Tags.Add(t);
}

return entity;
}

public TEntity Untag<TEntity>(TEntity entity, string str) where TEntity : ITaggable
{
Tag tag = entity.Tags.FirstOrDefault(x => x.Label == str);

if (tag != null)
entity.Tags.Remove(tag);

return entity;
}
}

我遇到的问题是它会创建重复项,我不明白为什么会生成数据库表。显然我的代码是错误的,但我不知道为什么以及在哪里。

var context = new Context();

var author = new Author {FirstName = "Stephen", LastName = "King"};
var book = context.Books.Add(new Book
{
Title = "Title",
Author = author,
Genre = "Sci-Fi"
});

var service = new TagService(context);
service.Tag(book, "best");
service.Tag(author, "best");

context.SaveChanges();

因此,当我执行该代码时,我只想要 1 个应该是“最佳”的标签。相反,我有 2 个标签,都是“最好的”。谁能解释为什么,我该如何解决这个问题

更新以下是 EF 生成的内容:

public override void Up()
{
CreateTable(
"dbo.Books",
c => new
{
BookId = c.Int(nullable: false, identity: true),
Title = c.String(),
Genre = c.String(),
Author_AuthorId = c.Int(),
})
.PrimaryKey(t => t.BookId)
.ForeignKey("dbo.Authors", t => t.Author_AuthorId)
.Index(t => t.Author_AuthorId);

CreateTable(
"dbo.Authors",
c => new
{
AuthorId = c.Int(nullable: false, identity: true),
FirstName = c.String(),
LastName = c.String(),
})
.PrimaryKey(t => t.AuthorId);

CreateTable(
"dbo.Tags",
c => new
{
TagId = c.Int(nullable: false, identity: true),
Label = c.String(),
})
.PrimaryKey(t => t.TagId);

CreateTable(
"dbo.TagBooks",
c => new
{
Tag_TagId = c.Int(nullable: false),
Book_BookId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Tag_TagId, t.Book_BookId })
.ForeignKey("dbo.Tags", t => t.Tag_TagId, cascadeDelete: true)
.ForeignKey("dbo.Books", t => t.Book_BookId, cascadeDelete: true)
.Index(t => t.Tag_TagId)
.Index(t => t.Book_BookId);

CreateTable(
"dbo.TagAuthors",
c => new
{
Tag_TagId = c.Int(nullable: false),
Author_AuthorId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Tag_TagId, t.Author_AuthorId })
.ForeignKey("dbo.Tags", t => t.Tag_TagId, cascadeDelete: true)
.ForeignKey("dbo.Authors", t => t.Author_AuthorId, cascadeDelete: true)
.Index(t => t.Tag_TagId)
.Index(t => t.Author_AuthorId);

}

public override void Down()
{
DropIndex("dbo.TagAuthors", new[] { "Author_AuthorId" });
DropIndex("dbo.TagAuthors", new[] { "Tag_TagId" });
DropIndex("dbo.TagBooks", new[] { "Book_BookId" });
DropIndex("dbo.TagBooks", new[] { "Tag_TagId" });
DropIndex("dbo.Books", new[] { "Author_AuthorId" });
DropForeignKey("dbo.TagAuthors", "Author_AuthorId", "dbo.Authors");
DropForeignKey("dbo.TagAuthors", "Tag_TagId", "dbo.Tags");
DropForeignKey("dbo.TagBooks", "Book_BookId", "dbo.Books");
DropForeignKey("dbo.TagBooks", "Tag_TagId", "dbo.Tags");
DropForeignKey("dbo.Books", "Author_AuthorId", "dbo.Authors");
DropTable("dbo.TagAuthors");
DropTable("dbo.TagBooks");
DropTable("dbo.Tags");
DropTable("dbo.Authors");
DropTable("dbo.Books");
}

最佳答案

您不使用相同的标签实体,只使用标签内容。在 Tag 方法中,您总是创建具有新 TagId 的新实体。如果标签存在并使用该实体而不是创建新实体,则必须使用给定标签添加额外检查。例如:

if (entity.Tags.All(t => t.Label != tag))
{
var tagEntity = _context.Tags.FirstOrDefault(t => t.Label == tag);

if(tagEntity == null)
tagEntity = _context.Tags.Add(new Tag {Label = tag});
entity.Tags.Add(tagEntity);
}

关于c# - 使用 Entity Framework 实现标签功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20432188/

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