LINQ to SQL - mapping exception when using abstract base classes

前端 未结 5 1059
离开以前
离开以前 2020-12-08 03:20

Problem: I would like to share code between multiple assemblies. This shared code will need to work with LINQ to SQL-mapped classes.

I\'ve encountered the same issu

5条回答
  •  情书的邮戳
    2020-12-08 03:45

    I have encountered this problem many times in the past because we have a similar architecture in a framework that we use in our company. You may have noticed that if you use the declarative style LINQ queries you'll not encounter this problem. For example the following code will work:

    return (from i in db.GetTable() where i.Name = "Something").FirstOrDefault();
    

    However, since we are using dynamic filter expressions we couldn't use this method. The alternative solution is to use something like this:

    return db.GetTable().Select(i => i).Where(i => i.Name == "Something").SingleOrDefault();
    

    This solution solved our problem since we can inject a ".Select(i => i)" to the beginning of almost all expressions. This will cause the Linq engine not to look at the base class for the mappings and will force it to look at the actual entity class and find the mappings.

    Hope it helps

提交回复
热议问题