Adding table from another database to ASP.NET Dynamic Data + Entity Framework - asp.net

I have a table in another database I would like to scaffold via ASP.NET Dynamic Data and incorporate into my existing Entity Model - is there anyway to do this? (eg using a view or other mechanism or customize the view, edit or insert operations via ad-hoc SQL or stored procedures?)
I don't want to replicate the entire DynamicData sub-folder structure and create another entity model for just one table

I was able to solve this by manually creating an entity in the SSDL and CSDL sections of the .edmx file by using a DefiningQuery and then defining the EntitySets for my entity class
I also added insert / update / delete Function elements to the SSDL with inline SQL using the CommandText property
At this point I had enough to let the Designer map the CRUD methods to these inline SQL functions I defined
It's a little tricky but it works and the general approach opens up many possibilities I had not thought about

Related

QSqlTableModel on multiple tables

I am using Qt's Model View Programming on a database, where an object is represented using multiple tables. Assume the following object and coresponding database tables which perfectly fit my design:
TagObject
- id
- name
- usable
- information
tag_table
- id
- name
- usable
tag_info_table
- id_ref
- info
As you may see, the information property is separated into another table to prevent existence of NULL because this property is optional.
In the database I have a view which aggregates the values into one 'table' which can be queried using QSqlTableModel. Note that INSERTing data is not possible this way. As far as I could understand, the database design is not supported by Qt's classes, neither QSqlTableModel nor QSqlRelationalTableModel do support this. (Additionally QSqlQueryModel does not support inserts at all so this is out of question.)
Am I missing something? Is there any way to do this using Qt's SQL classes? Or is the only way to achive this subclassing QSqlQueryModel as pointed out here?
The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.
Edit: As for subclassing I found this reference as a nice entry point.
Ideally, the view should have appropriate triggers that will modify the underlying tables. Make the view writable and your problems go away: you can use a QSqlTableModel directly on that view, then.
Alternatively, you can have a QSqlTableModel for each table, and then write a custom proxy model that supports inserts and translates between the source models and forms the writable view. It'll be more work than writing the SQL triggers.

How can I set the schema dynamically for a Code First DbContext?

I have an EF6 Database First application that uses tables generated to an edmx from the dbo schema.
To update certain large pricing tables that work in concert with each other, I create new tables in a new schema based on the date, then inside a transaction, move the current tables to a backup schema, and move the new tables to the dbo schema. This is implemented using a new MetadataWorkspace created by reading the edmx file and changing the schema, and allows me to have two DbContexts where one works with the existing data in the dbo schema, and the other works with the new tables in the new schema. And works great for Database First!
See this SO article.
For CodeFirst, one can set the modelBuilder.HasDefaultSchema in OnModelCreating, but then the DbContext is locked down, and OnModelCreating is not called again for new DbContext instances, so whatever schema was set is now used for all such DbContexts for the duration of the application.
My question is – how can I dynamically change the DbContext with CodeFirst where I can have two DbContext, each using different schema? I cannot just define two DbContext derived classes since the schema name is dynamic.
Apparently this cannot be done but once since the DbContext is locked down and keeps the schema name. I plan to address this need by keeping the second schema name fixed rather than dynamic. Would be nice if could "clone" the locked down DbContext with a new schema name but currently not possible anyway that I have found. Closing.
You can set the the schema dynamically in EF6. You need to adjust the way you initialize your DBContext though.
I found most of what I was looking for here:
Multi-Tenant With Code First EF6

Symfony Mapping in Doctrine without Annotations or XML Files

a customer has an existing database. The schema is often changed within the database itself (e.g. he adds a new column).
My task is to develop an admin area with symfony that automatically reacts on table schema changes without modifying the application code. E.g. the customer adds a new column to table "MyEntity", and the application automatically generates a new column in the accordingly list view.
My approach is to dynamically map the table columns to the Entity class so that ALL Attributes and ALL Getters/Setters are generated dynamically from the table schema.
So is it possible to map the table columns in a Doctrine Entity without the use of Annotations or XML Files.
Something like:
class MyEntity{
public function generateMappingFromSchema($sTableName){...}
}
Please don't do that. Doctrine was not designed for such use case.
There is a library though you should check https://github.com/laravel-doctrine/fluent which basically is a mapping driver that allows you to manage your mappings in an Object Oriented approach. And there are other tools:
http://crud-admin-generator.com/
http://crudkit.com/
http://www.grocerycrud.com/
which are maybe better for that, I don't know.
But again, please don't do that. Do not allow the customer to modify the database schema or give them e.g. a phpMyAdmin which was designed for that.

MVC3 Entity Framework using default membership relationships

I want to create a relationship between a custom table (Websites) and the default aspnet tables related to Users.
I'm using code-first so for most FK relationships I would just do
public ModelName ModelName { get; set; }
With this, EF will automatically create the FK relationships. Very easy.
What's confusing is the most effective way to hook into the aspnet users/membership table. Do I create a new model Users that acts as an interface so that I can implement custom user code?
Is there a best way to do this that fits well into EF best practices? I basically just want to relate a user to the Websites table/model so that EF can do its thing.
"Do I create a new model Users that acts as an interface so that I can implement custom user code?"
If you want flexibility, I would say this is the way to go. This way it would be easier if you wanted to change to some sort of different Authentication DB structure in the future.
For example, have an "AppUser" Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way you can simply add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.
https://github.com/TroyGoode/MembershipStarterKit
Hope this helps!
This has few preconditions:
ASP.NET tables must be in the same database as your own tables
Previous precondition means that you must either create your database and tables manually (without automatic code-first generation) or you must use some custom initializer which will add non mapped ASP.NET tables as part of database recreation
If you want your model class to have relation with ASP.NET table you must model ASP.NET table as another entity. I'm not sure if you can use ASP.NET classes for that because for example MembershipUser doesn't have parameterless public constructor which is required for EF. So you will most probably need to create duplicate classes and their mappings and use these classes when referencing ASP.NET entities.

How do I define a database view using Entity Framework 4 Code-First?

How do I define a database view using Entity Framework 4 Code-First? I can't find anything about this anywhere!
That's because you cannot define database view using code-first approach. Database view is database construct which uses SQL Query on top of existing tables / functions. You can't define such constructs using code first.
If you want view you must create it manually by executing CREATE VIEW SQL script for example in custom initializer - it will be similar like this answer. Just be aware that this will not help you if you want to map entity to a view. In such case you would probably have to first drop table created by EF and create view with the same name (I didn't try it but it could do the trick). Also be aware that not every view is udpatable so you will most probably get read only entity.
To do a view you create the model, then in the initializer you run the SQL statement to create the view directly against the context with the first line of code, and then in the context you override OnModelCreating and run the second line of code to ignore the model.
context.Database.ExecuteSqlCommand(Resources.<resourcename>);
modelBuilder.Ignore<modeltype>();

Resources