简体   繁体   English

使用不同的外键指定一对多关系

[英]Specifying a one-to-many relationship using a different foreign key

Not sure if the title is good enough, but I'm going to try to explain the problem I'm encountering. 不知道标题是否足够好,但是我将尝试解释遇到的问题。 For the record, I'm very new to Code-First and Fluent API, and this is the first production project I'm using them in. 作为记录,我是Code-First和Fluent API的新手,这是我使用它们的第一个生产项目。

Basically, the web-app I'm trying to create is multi-lingual, and I decided to have a table with the following fields (I added comments to explain what I'm trying to achieve): 基本上,我要创建的Web应用程序是多语言的,因此我决定创建一个包含以下字段的表(我添加了注释以解释我要实现的目的):

public class Content
{
    [Key]
    // Required Key.
    public int Id { get; set; }

    [Required]
    // Language (another class that stores culture and other culture specific info).
    public Language Language { get; set; }

    [Required]
    // Text for the current field.
    [Column(TypeName = "ntext")]
    public string Text { get; set; }

    [Required]
    // The field Id, this stores what field the text above relates to.
    // So if I have a field "Welcome", and the website has 5 languages, I'll have 5 entries with the same GUID
    // but with different text and different language.
    public virtual Guid FieldId { get; set; }
}

I have been trying many solutions, but I haven't managed to create what I want. 我一直在尝试许多解决方案,但没有设法创建自己想要的东西。 My latest one is: 我最新的是:

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

    [Required]
    [Key]
    public Guid TitleId { get; set; }
    [Required]
    [Column("TitleId")]
    public virtual ICollection<Content> Title { get; set; }

    [Required]
    public decimal Value { get; set; }

    [Required]
    [Key]
    public Guid DescriptionId { get; set; }
    [Required]
    public virtual ICollection<Content> Description { get; set; }

and this in context (not sure if it makes sense): 和这在上下文中(不确定是否有意义):

modelBuilder.Entity<Winning>()
    .HasMany(x => x.Description)
    .WithRequired()
    .Map(x =>
    {
        x.MapKey("DescriptionId", "FieldId")
            .MapKey("TitleId", "FieldId");
    });

Basically what I want is that when I load the object above, through that GUID, I get a list of relating text (preferably just one, as I'd filter by culture), the table Content will be used by other object so a foreign key within Content does not solve my issue. 基本上我想要的是,当我通过该GUID加载上面的对象时,我得到了相关文本的列表(最好是一个,因为我按文化过滤),所以表Content将被其他对象使用,因此内容中的键不能解决我的问题。

Hm.. where to start. 嗯..从哪里开始。 :) :)

you don't need to have the ID of the Object and the Object both mapped. 您不需要同时映射对象的ID和对象的ID。 In fact EF won't like this. 实际上,EF不会这样。

Just something like this will get you the code first mapping that you want: 就像这样,您将获得所需的代码优先映射:

[Required] [Key] public Guid DescriptionId { get; [必需] [密钥]公共Guid DescriptionId { set; 组; } }

[Required]
public virtual ICollection<Content> Description { get; set; }

The fluent mapping you are trying to use is for many to many relationships. 您尝试使用的流利映射是多对多关系的。 You only need to specify fluent mappings if the code first migration doesn't work, or if you want to specify your own column or table names. 如果代码首次迁移不起作用,或者要指定自己的列名或表名,则仅需要指定流利的映射。 Once you remove your double mappings you probably won't need to specify anything special in fluent. 删除双重映射后,您可能无需指定任何特殊的流利语言。 Code first will just figure out what the column need to be and create them for you (unless you are working with an existing DB). 代码首先将仅弄清楚列的内容并为您创建列(除非您正在使用现有数据库)。 IF you find you still need fluent make sure you are using the correct mapping. 如果发现仍然需要流利,请确保使用正确的映射。

Lastly I don't think you can solve your problem using mappings. 最后,我认为您无法使用映射解决问题。 Map your entities so that the relate together. 映射您的实体,以便关联在一起。 Not sure what your second entity is named but here goes... 不确定第二个实体的名字,但是这里...

Winning has a collection of Titles and Descriptions? 获奖有标题和描述的集合吗? Then when you are displaying this information on a page you just need to use some code/logic in your controller to decide which title and description you will use in your view based on the current conditions of your application. 然后,当您在页面上显示此信息时,您只需要在控制器中使用一些代码/逻辑即可根据应用程序的当前条件决定在视图中使用哪个标题和描述。 Maybe you have something in your controller method to say what language you need etc. 也许您的控制器方法中有一些要说的语言,等等。

Hope that gets you back on the right track. 希望您能回到正确的轨道上。

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

相关问题 NHibernate单向一对多关系不保存外键 - NHibernate unidirectional one-to-many relationship not saving foreign key 如何在与EF映射的一对多关系中更新外键? - How to update the foreign key in a one-to-many relationship mapped with EF? 使用 EF Core 与复合主键建立一对多关系 - One-to-many relationship with composite primary key using EF Core 如果关系是一对多,如何在我的外键列上插入值 - How can I insert value on my Foreign Key Column if the relationship is one-to-many 实体框架 - 在Seed()方法中实现一对多外键关系 - Entity Framework - implementing one-to-many foreign key relationship in Seed() method 实体框架一对多关系-当外键名称命名为ID时,如何重命名? - Entity framework one-to-many relationship - how to rename foreign key name when it is named Id? 保存记录时无法在外键中插入null(一对多关系) - Cannot insert null in foreign key when saving record (one-to-many relationship) asp.Net Core 一对多关系 UPDATE 语句与 FOREIGN KEY 约束冲突 - asp.Net Core one-to-many relationship UPDATE statement conflicted with the FOREIGN KEY constraint EF Core与其他外键资产之间的一对多关系 - EF Core one-to-many relationship beside another foreign key property 一对多关系在asp.net中将外键保存为NULL - one-to-many relationship saves foreign key as NULL in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM