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

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

问题描述

我正在处理一些具有关系的旧表,但这些关系尚未明确设置为主键/外键.我使用Linq To Sql Classes"创建了一个 .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不能作为 Cases 的成员使用.

我从同事那里听说我可能会尝试使用 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. 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 To SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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