Hi i have made a data sync project on top of entity framework.
the framework is schema independent to some extent.
i want to make it more tolerant to changes in schema even the currently considered breaking changes.
to achieve this i will have to get inside the ef migration engine and will have to generate a command like
add-transformation
which will be detecting the changes and creating a transformation.
I have looked into the source code of ef 6 but couldnt find an appropriate place to start.
any help would be appreciated.
Edit 1 :- answer to questions received in the comments
Code First Approach
Extent:
Changes in data will be handled by the migration so no need to incorporate the changes.
What I need to is a way to execute a command like add-transformation which would create a new transformation like a new migration. So typically lets say i have a database model (domain model) like
class A
{
public int a {get; set;}
public int b {get; set;}
}
then i change the class to the structure
class A
{
public int a {get; set;}
public int b {get; set;}
public int c {get; set;}
}
and then i run add-tranformation ClassChangesA
the code i require should
1. Detect changes
2. Generate a class like the migration class. Ex.
class Transformation_112334_ClassChangesA
{
public A Up(OldA model){
//Property C added
}
public OldA Down(A model){
//Property C removed
}
}
I believe that the command you are looking for is add-migration migration_name
then you can update your database using the command update-database, this is how to work with code first migrations in entity framework.
Related
I have decided to use code first migrations. I did not use them before. I have, in the past just created the models and then added the tables as needed to the database. I use several SQL Views in my project. I create the SQL View and then create a model for it. It works fine, however when I do migrations it treats them like tables and adds them to the database as a table.
I could use the
modelBuilder.Ignore<AspNetUsers>();
for the view, but is there any way to actually create the SQL view from the model? Below is one of my SQLView entities
[NotMapped]
public IDbSet<StockReportView> StockReportView { get; set; }
If not then I will just continue to create the SQL views and create the model and then add it to the OnModelCreating as modelBuilder.Ignore<StockReportView>();
Thanks for all your help.
UPDATE:
I found a question that was closed but had some answers. I Implemented what they said. Which was:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SomeProject.Data
{
[Table("SomeView")]
public class SomeView
{
[Key]
public int NameID { get; set; }
public string Name { get; set; }
}
}
This deleted all of the tables it created That were SQL Views, But did not create the SQLViews.
I have the following model:
I want to expose a member on "intallations" that give me a list of "modules" based on the join table "installation_modules", how can I do that ?
I want to be able to write
installations.Modules.Something()
Without having to use the join table in my code.
I also want to map "installation_type" directly on the installation, is it possible ? If yes how ?
Entity Framework will automatically manage the many-many relationship for you, but usually the installation_modules table should be having just two columns, installation_id and module_id which will be the composite primary key instead of a separate primary key. So Installation model/class will have public virtual ICollection<Module> Modules {get; set;} and the Module class will have public virtual ICollection<Installation> Installations {get; set;}navigational properties for easy accessing of entities.
I also want to map "installation_type" directly on the installation, is it possible ? If yes how ?
Yes, it is possible. You can have a navigational property for InstallationType in your Installation entity for this.
public class Installation
{
//....other properties
[Column("installation_type_id")]
public int InstallationTypeId {get; set;}
[ForeignKey("InstallationTypeId")]
public virtual InstallationType InstallationType{get; set;}
}
Since I already have a DB project in my solution, I was wondering can I leverage Entity Framework so that the Web Application may communicate with the database without having to do any migrations, or is migration necessary in order to use the Context of Entity Framework?
If migrating is not necessary then I also won't need the designer (edmx) file, I could just make my models and link them into my context right?
Not necessary at all.
I often write short solutions towards some databases where I just want to add some functionality on the fly. What I typically do is to just open SQL Server Object Explorer in VS2015, Expand the table I'm interested in, and just write a class on the fly, matching the fields that I need.
I.e for a table named "Classes" in the database, I would end up with a class in C#
public class SomeClass
{
public int Id {get; set;}
public string Title {get; set;}
// +other intersting fields
}
Then I simply create a DbContext class pointing to my database:
public class MyContext : DbContext
{
public DbSet<SomeClass> Classes{get; set;}
public MyContext() : base("myConnectionString"){}
}
That's it. No need for migrations. It then simply just works :)
I’m putting together a code first model that has a lot of reference data. This model is based around applications, servers, and build deployments. Thus, there are a lot of many to many relationships. The pain that I’m feeling is that new records are being placed in the entity tables which I’m attempting to use as reference data. For example, we have a list of servers. I only want to see a server ONCE in the table. For all the entities referring to that server, I want them to use that row. The same can be said of my ServerRoles and Applications tables. These tables contain static data that I’m seeding and should rarely change.
I know I could solve this with look-ups and hand wiring, but I would think EF would comprehend this scenario.
Using Entity Framework code-first you can create an immutable object with protected parameter less constructor and private set properties.
It works for sure with EF 5 Beta.
[Update]
Tested also with EF 4.3.1, it works.
public class Nation
{
protected Nation() {}
public Nation(Guid id, string name)
{
Id = id;
Name = name;
}
public Guid Id { get; private set; }
public string Name { get; private set; }
}
How to create an entity type and then generate a database table from it?
I know this feature was not supported two years ago in EF, what about now?
You've got 2 options:
Entity Framework Model First where you create the model first and then generate the database from that or
Entity Framework Code First where you create normal Poco objects and generate the database from that.
I've personally used Entity Framework Code First for MVC development and it works like a charm, it really is an awesome feature and easy to use.
Now, Entity Framework introduced these feature.
Basically, with only two steps is sufficient for this, please see below steps to go:
Create your Entity
public class Resturant
{
public int ID { get; set; }
public string Name { get; set; }
}
Create Context class
public class OdeToFoodDb: DbContext
{
public DbSet<Resturant> Resturants { get; set; }
}
However, you may need more coding in Global.ascx for advance options but these are the basic steps.
A database named "OdeToFoodDb" will create and a table named "Resturant" also will create by these steps.