LINQ to SQL - mapping exception when using abstract base classes

前端 未结 5 1050
离开以前
离开以前 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:31

    You're asking several questions here Jarrod, can you be more specific? That is, do you just want to know why your method fails? Or maybe you want a way of using data objects across different projects? I'm assuming you're not trying to use LINQ to SQL as a database mapping layer and that you are using it as a domain model? In which case, do both applications implement the same domain (business processes, validation, etc.)?

    0 讨论(0)
  • 2020-12-08 03:36

    I've had luck defining data classes in a shared assembly and consuming them in many assemblies versus mapping many assemblies' data classes to a shared contract. Using your example namespaces, put a custom DataContext and your shared data classes in TestLinq2Sql.Shared:

    namespace TestLinq2Sql.Shared
    {
        public class SharedContext : DataContext
        {
            public Table<User> Users;
            public SharedContext (string connectionString) : base(connectionString) { }
        }
    
        [Table(Name = "Users")]
        public class User
        {
            [Column(DbType = "Int NOT NULL IDENTITY", IsPrimaryKey=true, CanBeNull = false)]
            public int Id { get; set; }
    
            [Column(DbType = "nvarchar(40)", CanBeNull = false)]
            public string Name { get; set; }
    
            [Column(DbType = "nvarchar(100)", CanBeNull = false)]
            public string Email { get; set; }
        }
    }
    

    Then consume the DataContext from any other assembly:

    using (TestLinq2Sql.Shared.SharedContext shared = 
        new TestLinq2Sql.Shared.SharedContext(
            ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString))
    {
        var user = shared.Users.FirstOrDefault(u => u.Name == "test");
    }  
    
    0 讨论(0)
  • 2020-12-08 03:38

    Try including OfType before Where clause

    return _dbContext.GetTable<T>().OfType<T>().Where(expression).ToList();

    0 讨论(0)
  • 2020-12-08 03:40

    This looks like a bug - we special case Single on a primary key to do a local lookup but it looks like this code path is not grabbing the metadata properly.

    The 1=1 hack will mean it goes via a normal database round-trip but really a bug should be filed...

    0 讨论(0)
  • 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<TUser>() 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<TUser>().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

    0 讨论(0)
提交回复
热议问题