【问题标题】:Set foreign key value in business entity data model在业务实体数据模型中设置外键值
【发布时间】:2009-07-29 12:25:31
【问题描述】:

我有具有 SubLocationName 和 LocationId 的表 SublocationTbl(此字段是来自表 LocationTbl 的外键),并且我有业务实体数据模型 我知道外键不会出现在业务实体中 我的问题是:当我需要添加新的 subLocation 时,如何设置 locationId 的值?

【问题讨论】:

    标签: asp.net linq


    【解决方案1】:

    如果你想查看它自己的外键,你必须在 .NET 3.5 中使用 .NET 框架 4,你会发现对外键类的引用而不是它自己的外键

    Store s = new Store(); User u=s.User;//bring referance of the user with foreign key .net 3.5 & 4 int i = s.User_ID;//bring the user_id forein key .net 4

    【讨论】:

      【解决方案2】:

      像这样...?

      using(var db = new myDbContext())
      {
          var myNewSubLocation = new SubLocation();
          myNewSubLocation.LocationID = 12345; //put ur value here
      
          db.SubLocations.Add(myNewSubLocation);
          db.SubmitChanges();
      }
      

      【讨论】:

      • 问题是 locationId 是外键,它不会出现在子位置业务实体中,所以 myNewSubLocation.LocationID 不存在
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 2016-10-19
      • 2021-02-05
      • 2018-10-25
      相关资源
      最近更新 更多