gpt4 book ai didi

entity-framework - Entity Framework : Assign entity to property of another entity

转载 作者:行者123 更新时间:2023-12-04 07:03:20 26 4
gpt4 key购买 nike

我有这些实体(这只是我为这篇文章创建的抽象):

  • 语言
  • 说明

  • 这些是它们之间的引用:
  • * - 1 语言
  • 说明 * - 1 语言
  • 1 - 1 说明

  • 如果我这样取:
    var myFetch = from c in context.Districts
    where c.Id = 10
    select new { DistrictId = c.Id, Lang = c.Language };

    之后,我尝试将其分配给 说明 像这样:
    Description desc = Description.CreateDescription(0, "My description");
    desc.DistrictReference.EntityKey = new EntityKey("MyEntities.Descriptions", "DistrictId", myFetch.DistrictId);
    desc.Language = myFetch.Lang; //throws error

    抛出的错误是:

    System.InvalidOperationException: The relationship cannot be defined because the EntitySet name 'MyEntities.Descriptions' is not valid for the role 'District' in association set name 'MyEntities.District_Description'.



    我究竟做错了什么?

    最佳答案

    如果 myFetch是类 District 的一个实例你可以以编程方式做到这一点:

    desc.DistrictReference.EntityKey = new EntityKey(  
    String.Format(
    "{0}.{1}",
    myFetch.EntityKey.EntityContainerName,
    myFetch.EntityKey.EntitySetName),
    "DistrictId",
    myFetch.DistrictId);

    关于entity-framework - Entity Framework : Assign entity to property of another entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1498405/

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