Custom Linq-to-SQL MappingSource

I have a project that mixes a compiled entity framework domain with dynamically defined external databases. Users can provide a connection string, table name, and column names at run time to access an external system. I used to use dynamically created SQL to access an external system (of course, with care to avoid SQL injection), and read the tables of interest in the DataTable and materialized them in POCO.

Now 90% of my views used IQueryable objects to check the structure of database queries and formatting / page / efficient view management. This allowed me to do enough work with the request before executing it. For views that are related to recordings from external systems, I just gritted my teeth and used IEnumerable.AsQueryable() .

I would very much like to use a different solution. I'm leaning towards Linq-to-SQL (I'm open to other suggestions), but I'm fixated on creating a dynamic MappingSource . In addition, some columns are optional, and I don't know if Linq-to-SQL will explode if I did not provide a mapping for each column or not.

 public class ExternalMappingSource : MappingSource { public string[] KeyColumns { get; set; } public string NameColumn { get; set; } public string DescriptionColumn { get; set; } public string TimestampColumn { get; set; } protected override MetaModel CreateModel(Type dataContextType) { // ??? } } 

I could not find good resources in this online mode. How to clear this class?

+4
source share
1 answer

If you know the corresponding structure up (table and field values), you can submit a mapping to the user source based on this data without having a predefined object. For me, for another reason, I ended up using the XmlMappingSource object because it has a .FromXml () method that you can use to feed in arbitrary Xml. You can feel the Xml structure needed to validate the mapping file created by sqlmetal:

  • open the VS command prompt (in the "Visual Studio Tools" folder in the "Start" menu).
  • Change the directory to your project.
  • Enter the command "sqlmetal / map: whatever.map/code yourlinqtosql.dbml". This generates a what.map file with formatted Xml.

Program as needed for use in the XmlMappingSource.FromXml () method. Then you can create your context with the corresponding connection string and the changed mapping source.

+4
source

All Articles