【问题标题】:Entity Framework - Multiple tables referencing to one table实体框架 - 多个表引用一个表
【发布时间】:2015-02-01 22:05:52
【问题描述】:

我来到这里时遇到了一个实体框架问题,我已经为此苦苦挣扎了一段时间。让我们快速描述一下。我有 2 个模型引用了一个模型...我真的不知道如何创建与 EF 注释的关系。

第一个模型:

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

    // Some other data

    public virtual LogLocation Location { get; set; }
}

第二个模型:

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

    // Some other data

    public virtual LogLocation Location { get; set; }
}

以及我所引用的模型:

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

    [ForeignKey("QueuedLog")]
    public int QueuedLogId { get; set; }

    [ForeignKey("ProcessedLog")]
    public int ProcessedLogId { get; set; }

    // Some other data

    public virtual QueuedLog QueuedLog { get; set; }
    public virtual ProcessedLog ProcessedLog { get; set; }
}

如您所见,我已经尝试过做某事,但无法正常工作。我收到一个错误:

LogLocation_ProcessedLog_Source::多重性在关系“LogLocation_ProcessedLog”中的角色“LogLocation_ProcessedLog_Source”中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是“”。 LogLocation_QueuedLog_Source::多重性在关系“LogLocation_QueuedLog”中的角色“LogLocation_QueuedLog_Source”中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是''。

只有在我建立典型的一对一关系时才有效——但这不是我想要的。

顺便说一句。这是我在 StackOverflow 的第一篇文章,所以我想和大家打个招呼! :) 您正在创建一个很棒的社区,感谢您的所有工作!

编辑 问题是:我怎样才能创建这些模型和关系,所以它会像这样工作:

我将新的 ProcessedLog 添加到 Db -> 它添加了一个新的 LogLocation,ProcessedLogId 等于相关的 ProcessedLog 并且 QueuedLogId 为 NULL。

【问题讨论】:

  • 有什么问题?
  • “只有当我这样做时它才有效……但这个不适合我” 为什么不呢?更加详细一些。您收到什么错误消息?
  • 对不起,伙计们,我还是编程新手,所以我的问题也不是很好,因为我不太了解所有内容:) 我编辑了帖子并添加了错误和最终我想回答的问题。

标签: c# sql sql-server entity-framework entity


【解决方案1】:

你可以这样做。数据库方面,我不确定这是否是一个很好的解决方案......虽然它看起来确实适合你。

它适用于 EntityFramework 6.1.3 和 SQL Server Express。

ProcessedLog.cs

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

    // Some other data

    public virtual LogLocation Location { get; set; }
}

QueuedLog.cs

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

    // Some other data

    public virtual LogLocation Location { get; set; }
}

LogLocation.cs

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

    // Some other data

    public virtual QueuedLog QueuedLog { get; set; }
    public virtual ProcessedLog ProcessedLog { get; set; }
}

上下文.cs

public class Context : DbContext
{
    public DbSet<ProcessedLog> ProcessedLogs { get; set; }
    public DbSet<QueuedLog> QueuedLogs { get; set; }
    public DbSet<LogLocation> LogLocations { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ProcessedLog>()
                    .HasRequired(p => p.Location)
                    .WithOptional(l => l.ProcessedLog);

        modelBuilder.Entity<QueuedLog>()
                    .HasRequired(p => p.Location)
                    .WithOptional(l => l.QueuedLog);
    }
}

还有我测试过的代码

using (var context = new Context())
{
    var processedLog = new ProcessedLog
    {
        Location = new LogLocation()
    };

    var queuedLog = new QueuedLog
    {
        Location = new LogLocation()
    };

    context.ProcessedLogs.Add(processedLog);
    context.QueuedLogs.Add(queuedLog);

    context.SaveChanges();
}

【讨论】:

  • 对不起,我忘记回复你的评论了。非常感谢! :)
猜你喜欢
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
相关资源
最近更新 更多