gpt4 book ai didi

c# - 没有双联表的多对多关系, Entity Framework

转载 作者:太空狗 更新时间:2023-10-29 21:59:34 25 4
gpt4 key购买 nike

我有以下设置(效果很好)。使用 CodeFirst (CTP4)。

一个模板有一个影响列表,每个影响都赋予一个特征一个值。

public class Template
{
public virtual int Id { get; set; }
public virtual ICollection<Influence> Influences { get; set; }
}

public class Influence
{
public virtual int Id { get; set; }
public virtual Trait Trait { get; set; }
public virtual int Value { get; set; }
}

public class Trait
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

模板是这样配置的。

public class TemplateConfiguration : EntityConfiguration<Template>
{
public TemplateConfiguration()
{
HasKey(o => o.Id);
Property(o => o.Id).IsIdentity();

HasMany(o => o.Influences).WithRequired()
.Map("templates.influences",
(template, influence) => new {
Template = template.Id,
Influence = influence.Id
});
MapSingleType(o => new {
o.Id
});
}
}

有效,但我宁愿避免使用额外的“影响”表。本质上,“影响”只是一个对象,不需要为它们建立中央存储。事实上,如果没有中央表,它对我试图接近的设计更有好处。

我希望为模板表设置一个这样的场景...基本上影响没有自己的表,它们只是由使用它们的特征/值映射。

    public TemplateConfiguration()
{
HasMany(u => u.Influences).WithMany()
.Map("templates.influences",
(template, influence) => new {
Template = template.Id,
Trait = influence.Trait.Id,
Value = influence.Value
});

MapSingleType(c => new {
c.Id
}).ToTable("templates");
}

当我尝试这样做时,我在模板映射上遇到了异常。

System.InvalidOperationException was unhandled

The given expression includes an unrecognized pattern 'influence.Trait.Id'.


如果需要,下面是整个项目代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.EntityClient;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace EFTest
{
class Program
{
static void Main(string[] args)
{
Database.SetInitializer<SampleDataContext>(new AlwaysRecreateDatabase<SampleDataContext>());
var builder = new ModelBuilder();

builder.Configurations.Add(new TraitConfiguration());
builder.Configurations.Add(new InfluenceConfiguration());
builder.Configurations.Add(new TemplateConfiguration());

var model = builder.CreateModel();

using (var context = new SampleDataContext(model))
{
var traits = new List<Trait>
{
new Trait { Name = "Years" },
new Trait { Name = "Days" }
};
traits.ForEach(x => { context.Traits.Add(x); });
context.SaveChanges();

var templates = new List<Template>
{
new Template
{
Influences = new List<Influence>
{
new Influence
{
Trait = context.Traits.Single( i => i.Name == "Years" ),
Value = 5
},
new Influence
{
Trait = context.Traits.Single( i => i.Name == "Days" ),
Value = 15
}
}
}
};
templates.ForEach(x => { context.Templates.Add(x); });
context.SaveChanges();
}
}
}

public class SampleDataContext : DbContext
{
public SampleDataContext(DbModel model)
: base(model)
{
}

public DbSet<Trait> Traits { get; set; }
public DbSet<Influence> Influences { get; set; }
public DbSet<Template> Templates { get; set; }
}

public class Trait
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

public class TraitConfiguration : EntityConfiguration<Trait>
{
public TraitConfiguration()
{
HasKey(o => o.Id);
Property(o => o.Id).IsIdentity();

MapSingleType(o => new {
o.Id,
o.Name
});
}
}

public class Influence
{
public virtual int Id { get; set; }
public virtual Trait Trait { get; set; }
public virtual int Value { get; set; }
}

public class InfluenceConfiguration : EntityConfiguration<Influence>
{
public InfluenceConfiguration()
{
HasKey(o => o.Id);
Property(o => o.Id).IsIdentity();

HasRequired(o => o.Trait);
Property(o => o.Value);

MapSingleType(o => new {
o.Id,
Trait = o.Trait.Id,
o.Value
});
}
}

public class Template
{
public virtual int Id { get; set; }
public virtual ICollection<Influence> Influences { get; set; }
}

public class TemplateConfiguration : EntityConfiguration<Template>
{
public TemplateConfiguration()
{
HasKey(o => o.Id);
Property(o => o.Id).IsIdentity();

HasMany( o => o.Influences).WithRequired()
.Map("templates.influences",
(template, influence) => new {
Template = template.Id,
Influence = influence.Id
});
MapSingleType(o => new {
o.Id
});
}
}
}

最佳答案

好的,新的一天新的想法。

我现在已经安装了 CTP4 并获得了与您相同的 4 个表。

之所以会产生多对多关系,是因为模型不知道一个影响只会被一个模板使用。然后你必须告诉它:

public class Influence
{
public virtual int Id { get; set; }
public virtual Trait Trait { get; set; }
public virtual int Value { get; set; }
public virtual Template Template { get; set; }
}

和:

    public InfluenceConfiguration()
{
HasKey(o => o.Id);
Property(o => o.Id).IsIdentity();
Property(o => o.Value);

MapSingleType(o => new
{
o.Id,
Trait = o.Trait.Id,
o.Value,
Template = o.Template.Id
});
}

影响表将如下所示:

CREATE TABLE [dbo].[Influences](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Template] [int] NULL,
[Trait] [int] NULL,
[Value] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Influences] WITH CHECK ADD CONSTRAINT [Influence_Template] FOREIGN KEY([Template])
REFERENCES [dbo].[Templates] ([Id])
GO

ALTER TABLE [dbo].[Influences] CHECK CONSTRAINT [Influence_Template]
GO

ALTER TABLE [dbo].[Influences] WITH CHECK ADD CONSTRAINT [Influence_Trait] FOREIGN KEY([Trait])
REFERENCES [dbo].[Traits] ([Id])
GO

ALTER TABLE [dbo].[Influences] CHECK CONSTRAINT [Influence_Trait]
GO

关于c# - 没有双联表的多对多关系, Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3772249/

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