Question

We have a ASP.NET MVC application with Linq2Sql and a SQL Server-Backend. The application is run on the main site of our customer, but every site has their own database in SQL Server. (Due to different reasons, they shouldn't share most of the information between each other.) However some general information is shared, this is stored in a Shared-Database, and every site-specific database has views which represent those tables in their respective databases.

For example when I have Sites S1, S2, S3 with their databases D1, D2, D3 and a shard database DS with a shared table TS

now in the databases S1-S3 I'll have a View which underlying query is simply:

SELECT * FROM DS.TS;

Having it written like this, SQL Server somehow automagically propagates all inserts, updates and deletes to DS.TS without the need of explicit instead of triggers. Which makes our lives much easier, since we also need to handle only one connection to one database and don't need to bother with two different databases.

While we write our Delete and Update commands ourselves in the application and don't use Linq2Sql, they work fine. However the insert command on the shared table is using InsertOnSubmit and fails with the following exception:

Can't perform Create, Update, or Delete operations on 'Table(TS)' because it has no primary key.
   at System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity)  

Is there any way to make this work, or will I have to create the Insert-Commands on those shared tables by myself and execute them with DbCommand.ExecuteNonQuery()

Was it helpful?

Solution

LINQ to SQL with views doesn't know which column contains the key. You may be able to tweak the generated model and only set the column(s) that should be primary keys as appropriate. Be aware in your view however that you shouldn't do select * as it may kill performance over time.

OTHER TIPS

You can set IsPrimaryKey property of the primary key in your model to True

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top