Linq到SQL没有显式外键关系 [英] Linq To SQL Without Explicit Foreign Key Relationships

查看:172
本文介绍了Linq到SQL没有显式外键关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些具有关系的遗留表,但是这些关系没有明确设置为主键/外键。我使用Linq To Sql类创建了一个.dbml文件,并建立了正确的Case.CaseID = CaseInfo.CaseID关联。我的结果类是CasesDataContext。

I am working with a few legacy tables that have relationships, but those relationships haven't been explicitly set as primary/foreign keys. I created a .dbml file using "Linq To Sql Classes" and established the proper Case.CaseID = CaseInfo.CaseID association. My resulting class is CasesDataContext.

我的表(一对多):

Case
------------------
CaseID (int not null)
MetaColumn1 (varchar)
MetaColumn2 (varchar)
MetaColumn3 (varchar)
...


CaseInfo
------------------
CaseInfoID (int)
CaseID (int nulls allowed)
CaseInfoMeta (varchar)
...

我是LinqToSQL的新手,我遇到了麻烦。

I'm new to LinqToSQL and am having trouble doing..

CasesDataContext db = new CasesDataContext();
var Cases = from c in db.Cases
            where c.CaseInfo.CaseInfoMeta == "some value"
            select c;

(编辑)我的问题是,CaseInfo或CaseInfos
不可用

我听说一个同事,我可能会尝试ADO.Net实体数据模型来创建我的数据上下文类,但没有尝试了,想看看我是否会浪费我的时间,或者我应该去另一条路线。

I heard from a colleague that I might try ADO.Net Entity Data Model to create my Data Context class, but haven't tried that yet and wanted to see if I'd be wasting my time or should I go another route. Any tips, links, help would be most appreciated.

推荐答案

返回设计师并检查关系是否正确设置。这里是一个现实生活的例子,BillStateMasters有CustomerMasters1属性(状态的客户):

Go back to the designer and check the relation is set up correctly. Here is one real life example, with BillStateMasters have "CustomerMasters1" property (customers for the state):

Ps。正在清除命名...

Ps. naming is being cleaned up ...

更新1:。还需要确保两个表都有主要定义。如果主键未在数据库上定义(并且不能由于任何原因定义),请确保在设计器中定义它们。打开列的属性,并将其设置为主键。也就是说,如果您没有实体的主键,那么实体跟踪也将不起作用,对于删除,这意味着它不会更新实体。所以,确保审查所有实体,并让他们都有一个主键(如我所说,如果它不能在数据库,然后在设计器)。

Update 1: You also need to make sure both tables have a primary defined. If the primary key isn't defined on the database (and can't be defined for whatever reason), make sure to define them in the designer. Open the column's properties, and set it as primary key. That said, entity tracking also won't work if you haven't a primary key for the entity, which for deletes means it silently doesn't updates the entity. So, make sure to review all entities and to have them all with a primary key (as I said, if it can't be on the db, then on the designer).

这篇关于Linq到SQL没有显式外键关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆