简体   繁体   English

EF - Code First:在多对多关系中保存值的问题

[英]EF - Code First: Problem saving values in a many-to-many relationship

I'm trying to add a tagging system to a webapplication build on MVC3 with EF Code First for data access.我正在尝试将标记系统添加到基于 MVC3 构建的 Web 应用程序中,使用 EF Code First 进行数据访问。 The base is pretty simple.基础非常简单。 I have Projects, Companies and People who can have 1 or more tags.我有可以有 1 个或多个标签的项目、公司和人员。 I do not want to save the tags for each entity alone, so the tag class has 3 ICollections: People, Companies and Projects.我不想单独保存每个实体的标签,所以标签 class 有 3 个 ICollections:人员、公司和项目。 Each of the entities has an ICollection of tag.每个实体都有一个标签的ICollection。 see the code below看下面的代码

public class Project 
{
    public int ID { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Person
{
    public int ID { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Company
{
    public int ID { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    [Key]
    public int TagID { get; set; }
    public String Name { get; set; }

    public virtual ICollection<Project> Projects { get; set; }
    public virtual ICollection<Person> People { get; set; }
    public virtual ICollection<Company> Companies { get; set; }
}

In the context the mapping is set like this:在上下文中,映射设置如下:

modelBuilder.Entity<Tag>()
    .HasMany(t => t.Projects)
    .WithMany(p => p.Tags)
    .Map(m =>
    {
        m.MapLeftKey("ID");
        m.MapRightKey("TagID");
        m.ToTable("TagProjects");
    });


modelBuilder.Entity<Tag>()
    .HasMany(t => t.People)
    .WithMany(p => p.Tags)
    .Map(m => 
    {
        m.MapLeftKey("ID");
        m.MapRightKey("TagID");
        m.ToTable("TagPeople");
    });

modelBuilder.Entity<Tag>()
    .HasMany(t => t.Companies)
    .WithMany(p => p.Tags)
    .Map(m =>
    {
        m.MapLeftKey("ID");
        m.MapRightKey("TagID");
        m.ToTable("TagCompanies");
    });

Now when I try to add a tag to a project like this:现在,当我尝试向这样的项目添加标签时:

public void AddTagToProject(int tagId, int projectId)
{
    ProjectRepository projectRepository = new ProjectRepository();
    Project project = projectRepository.GetProjectByID(projectId);
    project.Tags.Add(GetTagByID(tagId));

    Save();
}

there is no exception, but the project has no tags added to it either.也不例外,但该项目也没有添加任何标签。 Anyone have an idea where to look?有人知道在哪里看吗?

public Tag GetTagByID(int tagId) 
{ 
    return db.Tags
        .Where(t => t.TagID == tagId)
        .First(); 
}

I've found the solution, the problem was that the project was not correctly updated and the values were never saved.我找到了解决方案,问题是项目没有正确更新并且值从未保存。

Have you made sure your database doesn't have any oddly named columns generated by EF 4.1?您是否确保您的数据库没有任何由 EF 4.1 生成的奇怪命名的列? I had that happen once, and the FK column was called UserTrialLicense_UserTrialLicenseID.我曾经发生过这种情况,FK 列被称为 UserTrialLicense_UserTrialLicenseID。 So anytime I tried to eager-load anything using.Include, it would not work.因此,每当我尝试使用.Include 急切加载任何内容时,它都行不通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM