在 ADO.NET 數據服務中添加對查找表的引用 (Adding a reference to a lookup table in ADO.NET Data Services)


問題描述

在 ADO.NET 數據服務中添加對查找表的引用 (Adding a reference to a lookup table in ADO.NET Data Services)

I’m attempting to use ADO.NET Data Services to update my database.

Tables:

  • Person( PK PersonId; FK EntityRootId)
  • EntityRoot( PK EntityRootId, FK EntityTypeId)
  • EntityTypes( PK EntityTypeId)

I’m trying to create an EntityRoot (unfortunate name since it may be confused with Entity Framework’s Entity.  EntityRoot is in my domain) and add it to the database:

var entRoot = EntityRoot.CreateEntityRoot(
    0, "Lou", DateTime.Now, "Lou", DateTime.Now);
var entityType = 
   (from type in myContext.EntityTypeSet
    where (type.Description == "Person")
    select type).FirstOrDefault(); // this works fine and returns the entityType I’m looking for

entRoot.EntityType = entityType;
myContext.AddToEntityRootSet(entRoot); 

// with the line below I get a runtime error:
//  “AddLink and DeleteLink methods only work when the sourceProperty is a collection.”
//myContext.AddLink(entRoot, "EntityType", entityType); 

// without the line above I get an error from the save:
//  “Entities in 'MyEntities.EntityRootSet' participate in the 'FK_EntityRoots_EntityTypeId_Lookup_EntityTypes' 
//    relationship. 0 related 'EntityTypes' were found. 1 'EntityTypes' is expected.”
myContext.BeginSaveChanges(SaveChangesOptions.Batch,
                      new AsyncCallback(OnSaveAllComplete),
                      null); 

How can I add a reference to the entRoot.EntityTypeId field?

Thanks for any insight into this.


參考解法

方法 1:

I think when doing this you should use SetLink instead of AddLink, because the property you are indicating is from Entity to EntityType (an Entity has one EntityType).

This is basically what the error message is telling you (AddLink works only on Collection properties, like the reverse property from type to entity, or for many to many relationships).

方法 2:

Is it just a typo in your question or should it be

myContext.AddLink(entRoot, "EntityTypes", entityType); 

At the start of your question you write "EntityTypes" (with and ending "s").

(by WeejDenis Trollersplattne)

參考文件

  1. Adding a reference to a lookup table in ADO.NET Data Services (CC BY-SA 3.0/4.0)

#.net-3.5 #wcf-data-services






相關問題

如何在 C# 中搜索 pdf 中的文本(執行匹配) (How to search text (Exect match) in pdf in C#)

String.IsNullOrEmpty() (String.IsNullOrEmpty())

是否有可以綁定的可為空的日期選擇器? (Is there a nullable datepicker that I can bind to?)

具有自動命名屬性的通用組合框 (Generic ComboBox with automatically named properties)

在數據庫未定義的外鍵關係上配置實體框架 (Configuring Entity Framework on a Database Undefined Foreign Key Relationships)

我可以在 4.0 應用程序中引用 .NET 3.5 .DLL 嗎? (Can I reference a .NET 3.5 .DLL in a 4.0 app?)

如何在 ASP.NET、VB.NET 中解決這個會話問題? (How to tackle this session problem in ASP.NET,VB.NET?)

在 ADO.NET 數據服務中添加對查找表的引用 (Adding a reference to a lookup table in ADO.NET Data Services)

如何優化 Linq to Xml 查詢反對屬性? (How do I optimize a Linq to Xml query againist attributes?)

VS2005 和 LINQ (VS2005 and LINQ)

時區信息錯誤? (TimeZoneInfo error?)

與標準 C++ 相比,C++/CLI(以前稱為“託管 C++”)有哪些優勢? (What are the advantages of C++/CLI (formerly "Managed C++") over standard C++?)







留言討論