具有一对多和多对多关系的 LINQ To SQL 预加载

发布于 2024-11-05 03:50:38 字数 2498 浏览 0 评论 0原文

我是使用 Linq to SQL 的新手,我遇到了一个问题

这是我的模型的一部分:

  • 访问 > >访问产品<产品
  • 参观>访问样品<产品
  • 参观> VisitStatus

我想立即加载所有相关实体的访问实体,但它给了我一个“InvalidCastexception”。 ,我使用此代码:

using (TeamsExcellenceDataContext context = new TeamsExcellenceDataContext())


{


            DataLoadOptions lo = new DataLoadOptions();
            context.DeferredLoadingEnabled = false;
            lo.LoadWith<Visit>(v => v.Visitstatus);
            lo.LoadWith<Visit>(v => v.VisitProducts);
            lo.LoadWith<Visit>(v => v.VisitSamples);

            context.LoadOptions = lo;
            var temp = (from v in context.Visits
                   where v.VisitID == visitID
                   select v).Single();
            Visit v = temp;
        }

生成的 SQL 语句分为 2 部分(我不知道为什么会发生这种情况?):

SELECT [t0].[VisitID], [t0].[StartDate], [t0].[EndDate], [t0].[TypeID], [t0].[StatusID], [t0].[NextObjective], [t0].[CpFeedback], [t0].[Feedback], [t0].[AccID], [t0].[CustMPID], [t0].[AccTreeID], [t0].[RepPermitDays], [t0].[ModeTypeID], [t0].[Recurrence], [t0].[RecurrenceParentKey], [t0].[CDate], [t2].[VisitID] AS [VisitID2], [t2].[ProductID], [t2].[VisitProductTurn], [t2].[CDate] AS [CDate2], (
    SELECT COUNT(*)
    FROM [dbo].[VisitProducts] AS [t3]
    WHERE [t3].[VisitID] = [t0].[VisitID]
    ) AS [value], [t1].[VisitStatusID], [t1].[VisitStatusName], [t1].[VisitStatusMarker], [t1].[CDate] AS [CDate3]
FROM [dbo].[Visits] AS [t0]
INNER JOIN [dbo].[Visitstatus] AS [t1] ON [t1].[VisitStatusID] = [t0].[StatusID]
LEFT OUTER JOIN [dbo].[VisitProducts] AS [t2] ON [t2].[VisitID] = [t0].[VisitID]
WHERE [t0].[VisitID] = @p0
ORDER BY [t0].[VisitID], [t1].[VisitStatusID], [t2].[ProductID]
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [411]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1

SELECT [t0].[VisiteID], [t0].[ProductID], [t0].[SampleQuantity], [t0].[CDate]
FROM [dbo].[VisitSamples] AS [t0]
WHERE [t0].[VisiteID] = @x1
-- @x1: Input Int (Size = -1; Prec = 0; Scale = 0) [411]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1/

如果我省略此行,则不会发生异常:

lo.LoadWith<Visit>(v => v.Visitstatus);

或这些行

lo.LoadWith<Visit>(v => v.VisitSamples);
lo.LoadWith<Visit>(v => v.VisitProducts);

我做错了什么或者如何解决这个问题?

I am new to using Linq to SQL and I have an issue

This is part of my model:

  • Visit > VisitProduct < Product
  • Visit > VisitSample < Product
  • Visit > VisitStatus

I want to eager load the visit entity with all related entities,But It gives me an "InvalidCastexception". ,I use this code:

using (TeamsExcellenceDataContext context = new TeamsExcellenceDataContext())


{


            DataLoadOptions lo = new DataLoadOptions();
            context.DeferredLoadingEnabled = false;
            lo.LoadWith<Visit>(v => v.Visitstatus);
            lo.LoadWith<Visit>(v => v.VisitProducts);
            lo.LoadWith<Visit>(v => v.VisitSamples);

            context.LoadOptions = lo;
            var temp = (from v in context.Visits
                   where v.VisitID == visitID
                   select v).Single();
            Visit v = temp;
        }

The generated SQL statement is split into 2 parts (I don't know why this occur?):

SELECT [t0].[VisitID], [t0].[StartDate], [t0].[EndDate], [t0].[TypeID], [t0].[StatusID], [t0].[NextObjective], [t0].[CpFeedback], [t0].[Feedback], [t0].[AccID], [t0].[CustMPID], [t0].[AccTreeID], [t0].[RepPermitDays], [t0].[ModeTypeID], [t0].[Recurrence], [t0].[RecurrenceParentKey], [t0].[CDate], [t2].[VisitID] AS [VisitID2], [t2].[ProductID], [t2].[VisitProductTurn], [t2].[CDate] AS [CDate2], (
    SELECT COUNT(*)
    FROM [dbo].[VisitProducts] AS [t3]
    WHERE [t3].[VisitID] = [t0].[VisitID]
    ) AS [value], [t1].[VisitStatusID], [t1].[VisitStatusName], [t1].[VisitStatusMarker], [t1].[CDate] AS [CDate3]
FROM [dbo].[Visits] AS [t0]
INNER JOIN [dbo].[Visitstatus] AS [t1] ON [t1].[VisitStatusID] = [t0].[StatusID]
LEFT OUTER JOIN [dbo].[VisitProducts] AS [t2] ON [t2].[VisitID] = [t0].[VisitID]
WHERE [t0].[VisitID] = @p0
ORDER BY [t0].[VisitID], [t1].[VisitStatusID], [t2].[ProductID]
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [411]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1

SELECT [t0].[VisiteID], [t0].[ProductID], [t0].[SampleQuantity], [t0].[CDate]
FROM [dbo].[VisitSamples] AS [t0]
WHERE [t0].[VisiteID] = @x1
-- @x1: Input Int (Size = -1; Prec = 0; Scale = 0) [411]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1/

The exception doesn't not occur if I omit this line:

lo.LoadWith<Visit>(v => v.Visitstatus);

OR any of these lines

lo.LoadWith<Visit>(v => v.VisitSamples);
lo.LoadWith<Visit>(v => v.VisitProducts);

What I am doing wrong or how can I work around this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒灬来后我 2024-11-12 03:50:38

问题是引发 InvalidCastexception 异常,因为表 Visitstatus 的主键是tinyint 类型,并且显然 Linq To SQl 对此 DB 数据类型有问题,所以我将tinyint 类型更改为 int 并且它有效!

因此,这提出了一个问题,如果我不允许更改数据库设计,或者我使用数据库类型tinyInt 至关重要,有什么解决方法吗?

The problem was that the exception InvalidCastexception is thrown cause the primary key for the table Visitstatus is of type tinyint and apparently Linq To SQl has a problem with this DB data type so I changed the tinyint type to int and It works !!!

So this raises A question here what if I am not allowed to change the Data Base Design or it is crucial that I use the Data Base Type tinyInt Is there any work around ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文