Auto generate DB with entity framework model first design - entity-framework-4.1

I have used both the EF code first and model first apporaches. In my case the model first approach has overall been better for my use. The only thing I miss from code first is that it would auto generate the mappings, and then use those mappings to create a database. Is their any way that I can get model first to do this same thing, namely create the mappings automatically when it instantializes the database.

The EDMX designer should handle this already. When you first generate a database from your model, EF will create the appropriate mappings for you.

Related

Where to put the calls to stored procedures which do not return an entity (Spring Data JPA)

I have the following Entity classes
Associate
Project
Allocation
Associate and Project are connected by an Allocation.
I have the standard structure of a spring boot application - the above entity classes, controllers, services, and repositories for each of the entities.
If I need to find/add/update/delete an Associate, I put the code in the Associate related #Repository and similarly for all the other entities.
I have a stored procedure call, that is a complex amalgamation of all the three tables (and returns columns in all thee tables), that logically doesn't come under any of the Entity classes (Its not simple like - all the Projects of an Associate, so that I can put it in Associate related classes)
So my question is - where do I put the call to the stored procedure?
Should I create a separate Entity class for the result type of the stored procedure and add its own controllers, services, repositories although I am pretty sure there is a better way of doing it.
I'd say it depends.
Having a separate repository for this the right approach if the result type and the algorithm implemented is really something separate from the original entities.
But in some cases it is really just one of the entities with some extra data in which case I'd put it in the repository of that entity.
In the first case note that nothing forces you to implement the repository using Spring Data. It is perfectly fine for such a case to have a manually implemented repository that is just normal bean with an EntityManager injected. This way it doesn't need an entity nor id type.
As Jen and others mentioned, if the procedure is returning the collection of data from one or more tables, in my project i kept this in one of the entity repository class where it is most suitable. I have also followed EntityManager approach like below in my service class
id = (Integer) entityManager.createNativeQuery("select get_next_global_id(?, ?)")
.setParameter(1, sequence)
.setParameter(2, status.toString())
.getSingleResult();

Symfony 4 - How to dynamically add field in an entity?

I want to have a form where I can add new fields (columns) in an specific entity. Is there a function for this?
Kind regards
Adding a whole column to the table through an HTML form is a weird use case.
If you want to stick to the ORM way of managing the persisted data, you'll have to dynamically add properties to existing entities, which might be a sign of bad schema design.
What I would guess you probably need is an automated way to add this column to your Entity. In such a case I would use the maker bundle.
Supposing that your Entity is called Employee, all you have to do is to type in the following command:
bin/console make:entity
When you'll be asked for the Entity name, enter Employee. The interpreter will tell you that this entity exists and if you want to extend it with news fields, and there you go.

Entity Framework 4.1 with existing classes AND existing tables

I have a large set of classes implementing business logic. Most have a .Load method that uses plain old ADO.net to read values from Sql Server that I've written by hand over the years. This all predates Linq2Sql and EF.
Now I want to update my class library to use Entity Framework but I'd like to do it as painlessly as possible. I've learned EF can infer column and key names from property names in my classes, but my classes have many properties that don't correspond to column names and some properties that don't match the database's column names. I'd prefer not to have to .Ignore() each of those properties (and remember to always .Ignore() any future properties) and .HasColumnName() all the differences.
What's the easiest way to use EF with existing tables AND existing classes so I can do minimal mappings and still use a DbContext to .Find() an entity and SaveChanges() and all the other nice strongly-typed things EF supports without going through all my business classes by hand and annotating which properties to include?
For example, I'd expect to be able to db.Customers.Find(123) and have it create a Customer instance, select * from customers where CustomerID=123, and map the columns that DO exist to the properties that DO exist as best as possible and give me a ready to use Customer instance and then I can annotate any differences as needed. Is this possible or am I asking too much of EF?
Is there maybe a smarter DbContext that will make a best effort to map properties so I can leverage all my existing business classes? Maybe I should consider some other ORM?
Try this:
create a data model (.edmx) from your database.
edit the model, Adding property and procedures of your Class That You want to add to the database.
Finally, update your database from your model (.Edmx) Selecting only the tables and procedures exist That You Have changes.
You can look at those tutorials
http://msdn.microsoft.com/en-us/data/gg685494
http://msdn.microsoft.com/en-us/data/gg685489
EF4 “Code First” enables you to optionally override its default database persistence mapping rules, and configure alternative ways to map your classes to a database.
There are a few ways to enable this. One of the easiest approaches is to override the “OnModelCreating” method defined on the DbContext base class:
public class YourContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Mapping
}
}
You can still search your entities by Primary Key by using Find:
var unicorn = context.Unicorns.Find(3);

Using same type for LINQ-to-SQL mapping on different tables

I have a LINQ-to-SQL data context in which two tables exist with different names but identical structures. One table (called CallRecords) holds live/current data, and the other (CallRecordsArchive) holds older records - but with the same field names as the live one.
With the basic mapping LINQ to SQL creates two classes CallRecord and CallRecordsArchive - but since they are the same I'd like to avoid this if possible? That way I don't have to write two queries for each instance?
I did consider creating a JOIN view but with millions of rows in both tables it would be a performance nightmare.
The way I've dealt with this is to create an interface for the common aspects of both tables and have both of the generated classes from your data context implement that interface through the use of the a partial class definition. This way when you want to deal with the type as a single concept you can always refer to it as the interface.
try to use inherit for this issue
check this link for more details.
one more
I hope it is help you.

Creating an AssociationSetMapping for MySQL with the Entity Framework Designer?

I am trying to build an Entity Framework model for my database schema using EF4 and Visual Studio 2010. Since I am stuck using MySQL as our database (for now), I pretty quickly discovered that since MYISAM tables don't support foreign key constraints, the designer does not have any way to automatically create all the associations for you. It is easy enough to go through the model and build them all manually, but I ran into a problem trying to map a pure join table.
Normally if you are using SQL Server or some other database that supports Foreign Keys, the designer will create all the mappings for you and in the case of pure join tables will create an AssociationSetMapping such that the join table is entirely hidden in the model. Instead you end up with a Many to Many mapping between two two primary entities.
However I am at a loss as to how to create this kind of mapping manually? If I create a Many to Many association between my two entities, I end up with a regular Association, not an AssociationSetMapping. There does not appear to be a way to create one of these in the designer than I can figure out, and to tell it which join table is supposed to be used at the database level.
Am I missing something, and there is a way to do this?
If not, I suppose I have to hack the XML directly, but at this point it is not clear what I need to add to the XML files and where, in order to manually create the association set mapping?
Ok, I can answer this one myself. Once you create a many to many association with the designer, you DON'T set a referential constraint on it, but rather use the Mapping Details window to tell the association what table is the join table, and which keys map to which fields in the database. Simple enough once I figured it out :) But I could not find any good documentation on this.