LINQ to SQL: Multiple / Single .dbml per project?

后端 未结 7 1149
悲哀的现实
悲哀的现实 2020-12-14 11:12

I\'ve read Rick Strahl\'s article on Linq to SQL DataContext Lifetime Management hoping to find some answers on how I would manage my .dbml files since they are so closely r

7条回答
  •  生来不讨喜
    2020-12-14 11:24

    Say you have a database:

    Database D contains tables A, B, C, X, Y, Z where

    • Table A has a foreign key relationship with tables B and C
    • Table X has a foreign key relationship with tables Y and Z
    • Table X also has a foreign key relationship with table A

    Say you have 2 DBML files P and Q based on database D

    • DBML File P contains entities A', B' and C' where A' is connected to B' and C' via associations.
    • DBML File Q contains entities X', Y' and Z' where X' is connected to Y' and Z' via associations.

    AFAIK, there is no way for DBML files P and Q to contain an association between entities A' and X'. This is the single biggest problem with having multiple DBML files.

    To my mind, a DBML file reflects the data-model represented by the tables and constraints on those tables in a database. If some tables or constraints are missing from a set of DBML files, then the set of DBML files do not accurately reflect the underlying database.

    Going back to our example, if there was no relationship between tables A and X in database D, then one would be able to create 2 DBML files.

    Generically speaking, one can have multiple DBML files if each DBML file contains all entities and relationships that are connected. Note that the converse is not a problem, i.e., one can have a single DBML file containing multiple groups of entities that are not related to each other by any associations.

提交回复
热议问题