On Insert / Update logic in EF code first - entity-framework

I would like to add some logic to the insert and update events of some EF objects.
I have a MVC application with category object which has a property which is a slugified version of the name property.
public class Category
{
public string Name { get; set; }
public string UrlName{ get; set; }
}
I would like to set the UrlName property only on the insert and update events because my slugify logic is quite elaborate.
I am aware that I can add some logic inside the SaveChanges() function on the context itself but I rather would like to put the code closer to the entity itself.
Is there a way to accomplish such thing using EF code first?

You can setup a base class with methods to be called before insert and update
public abstract class Entity
{
public virtual void OnBeforeInsert(){}
public virtual void OnBeforeUpdate(){}
}
public class Category : Entity
{
public string Name { get; set; }
public string UrlName{ get; set; }
public override void OnBeforeInsert()
{
//ur logic
}
}
Then in your DbContext
public override int SaveChanges()
{
var changedEntities = ChangeTracker.Entries();
foreach (var changedEntity in changedEntities)
{
if (changedEntity.Entity is Entity)
{
var entity = (Entity)changedEntity.Entity;
switch (changedEntity.State)
{
case EntityState.Added:
entity.OnBeforeInsert();
break;
case EntityState.Modified:
entity.OnBeforeUpdate();
break;
}
}
}
return base.SaveChanges();
}

No there is no such extension point because your entity is POCO - it is not aware of its persistence. Such logic must be triggered in data access layer which is aware of persistence. DbContext API offers only overriding of SaveChanges.
You can expose custom events or methods on your entities and call them during processing in SaveChanges.

Related

Adding New Objects with Entity Framework Repository Pattern

I am using Entity Framework and implementing the Repository pattern. Every example that I've been of adding new objects is something like this:
class MyRepository
{
public MyContext Context { get; set; }
public Add(MyObject myObject)
{
this.Context.MyObjects.Add(myObject);
}
public Save()
{
this.Context.SaveChanges();
}
}
// A window which lets the user add items to the repository
class MyWindow
{
private MyRepository Repository { get; set; }
private void DoSomething()
{
List<MyClass> myObjects = this.Repository.GetMyObjects();
// When I create a new object, I have to add the new object to the myObjects list and separately to the repository
MyClass newObject = new MyClass();
myObjects.Add(newObject);
this.Repository.Add(newObject);
// Do stuff to the objects in "myObjects"
this.Repository.Save();
}
}
What I want to be able to do is add new objects to the myObjects list (without having to add them to the repository on a separate line), and then just call something like this.Repository.Save(myObjects) when I'm ready to save them. Having to explicitly add every new object to the repository seems to break up the separation-of-concerns model. Is there a recommended way to do this, or is my reasoning flawed?
EDIT: DDiVita - I'm not sure what you mean by "attaching the entities to the context". This is what I'm currently doing in my Repository class:
public List<MyObject> GetMyObjects()
{
return this.Context.MyObjects.ToList();
}
Then in my Context class:
class MyContext : Context
{
public DbSet<MyObject> MyObjects { get; set; }
}
You can use the AddOrUpdate extension (the link is for Version 6 of EF) method on the DbSet. With this you can specify an identifier that EF will recognize as a unique value to either update or add the entity.
Let's assume your entity MyObject looks like this and the Id is always unique in your database:
public class MyObject
{
public int Id { get; set; }
public string Name { get; set; }
}
public Save(List<MyObject> myObjects)
{
this.Context.MyObjects.AddOrUpdate(m => m.Id,myObjects.ToArray());
this.Context.SaveChanges();
}
What you could do is use AddRange
public Save(List<MyObject> myObjects)
{
this.Context.MyObjects.AddRange(myObjects);
this.Context.SaveChanges();
}
And then your code could look like this
private void DoSomething()
{
List<MyObject> myObjects = this.Repository.GetMyObjects();
MyObject newObject = new MyObject();
myObjects.Add(newObject);
// Do stuff to the objects in "myObjects"
this.Repository.Save(myObjects);
}

Can't Get EF 6 Code First To Create the Tables

I already have a database with tables outside EF scope. But I want that the tables which will be used by EF to be created automatically.
public class SessionInfo
{
public Guid Id {get;set;}
public string Name { get; set; }
public DateTime StartsOn { get; set; }
public DateTime EndsOn { get; set; }
public string Notes { get; set; }
}
public class StudentsDbContext:DbContext
{
public StudentsDbContext():base("name=memory")
{
Database.Log = s => this.LogDebug(s);
}
public DbSet<SessionInfo> Sessions { get; set; }
}
This code just throws an exception because the table SessionInfoes doesn't exist.
using (var db = new StudentsDbContext())
{
db.Sessions.Add(new SessionInfo() {Id = Guid.NewGuid(), Name = "bla"});
var st = db.Sessions.FirstOrDefault();
}
What do I need to do so that EF will create the "SessionInfoes" (whatever name, it's not important) table by itself? I was under the impression that Ef will create the tables when the context is first used for a change or a query.
Update
After some digging, it seems that EF and Sqlite don't play very nice together i.e at most you can use EF to do queries but that's it. No table creation, no adding entities.
EF needs additional information in order to do this. You'll have to specify an IDatabaseInitializer first. Take a look at this list and find one that is appropriate for your needs (for example: MigrateDatabaseToLatestVersion, DropCreateDatabaseAlways, DropCreateDatabaseIfModelChanges, etc).
Then create your class:
public class MyDatabaseInitializer : MigrateDatabaseToLatestVersion
<MyDbContext,
MyDatabaseMigrationConfiguration>
Then also create the configuration for the initializer (ugh right?):
public class DatabaseMigrationsConfiguration
: DbMigrationsConfiguration<MyDbContext>
{
public DatabaseMigrationsConfiguration()
{
this.AutomaticMigrationDataLossAllowed = true;
this.AutomaticMigrationsEnabled = true;
}
protected override void Seed(MyDbContext context)
{
// Need data automagically added/update to the DB
// during initialization?
base.Seed(context);
}
}
Then one way to initialize the database is:
var myContext = new MyDbContext(/*connectionString*/);
Database.SetInitializer<MyDbContext>(new MyDatabaseInitializer());
myContext.Database.Initialize(true);
Some people prefer the to use the command line to migrate databases, but I don't want to assume I'll always have access to the database from a command lin.

EF Code First With Two DbContexts

This should be a simple one involving EF Code first but I can't wrap my head around the documentation and all the examples I am finding are from older versions. I am working with the latest (4.1).
Anyway I have some models like:
public class Foo
{
public int ID { get; set; }
public Bar Bar { get; set; }
}
public class Bar
{
public int ID { get; set; }
public string Value { get; set; }
}
I used some scaffolding with Asp.Net MVC to create my controllers/repositories and when I create a 'Foo' object, it also creates a 'Bar' object even though I set the 'Bar' property from something stored in the database.
public class FooViewModel
{
public int ID { get; set; }
public int BarID { get; set; }
}
public ActionResult Create(FooViewModel foo)
{
var entity = new Foo()
{
ID = foo.ID,
Bar = _barRepository.Find(foo.BarID)
};
_fooRepository.InsertOrUpdate(entity);
_fooRepository.Save();
// more stuff
}
How can I use fluent syntax for EF in order to stop it from creating a new 'Bar' row in the database?
Update
Here is the generated repository code:
public void InsertOrUpdate(Foo foo)
{
if (foo.ID == default(int)) {
// New entity
context.Foo.Add(foo);
} else {
// Existing entity
context.Foo(foo).State = EntityState.Modified;
}
}
public void Save()
{
context.SaveChanges();
}
your _fooRepository and _barRepository need to share same DB context instance. If the are using two instances the Bar will be in added state.
The problem must be somewhere in your repository layer - using the same model directly with EF 4.1 produces the expected result - a new row in the Foos table with a bar FK column pointing to the existing Bar.

Entity Framework 4.1 insert error

i have written a generic repository for my base windows which have a problem with.
lets be more specific, there is a little poco class called Unit as following:
public class Unit : BaseEntity
{
public string Name { get; set; }
private ICollection<Good> _goods;
public virtual ICollection<Good> Goods
{
get
{
if(_goods==null)
{
return new List<Good>();
}
return _goods;
}
set { _goods = value; }
}
}
which is inherited from a base entity class as :
public class BaseEntity
{
public int Id { get; set; }
public override string ToString()
{
return Id.ToString();
}
}
and this is my Add section of generic repository class:
public void Add(TEntity entity)
{
if (entity == null) return;
if (Context.Entry(entity).State == EntityState.Detached)
{
Context.Set<TEntity>().Attach(entity);
}
Context.Set<TEntity>().Add(entity);
Context.SaveChanges();
}
before add a new record, max id is fetched from db and placed in IdTextBox and them add method of base form is called which calls aforementioned Add method of base repository. here is the problem, i get this error, "The property 'Id' is part of the object's key information and cannot be modified."
there is also a mapper class that maps every property to its corresponding control which does its job fine.
What is my problem?
Thanks in advance.
i figured out that this problem is occured because of auto detect changes enability which was true.

Using enums with Entity Framework 4.1 code first

I am using entity framework 4.1 code first.
I have a GrantApplication class:
public class GrantApplication
{
// Just some of the properties are listed
public int Id { get; set; }
public GrantApplicationState GrantApplicationState { get; set; }
}
GrantApplicationState is an enum and looks like this:
public enum GrantApplicationState
{
Applying = 1,
Submitted = 2,
cknowledged = 3
}
Just before I go and add the grant application the database I set the grant application state:
public void Insert(GrantApplication grantApplication)
{
// Set the current state to applying
grantApplication.GrantApplicationState = GrantApplicationState.Applying;
// Insert the new grant application
grantApplicationRepository.Insert(grantApplication);
}
In my database I have a GrantApplication table with a GrantApplicationStateId that links to a GrantApplicationState table.
How do I get EF to add the state id from GrantApplication.GrantApplicationState to the GrantApplicationStateId column? Is this possible? And when I retrieve the GrantApplication object then it will need to be set as well. Is this the way to do it or do I have to create another property in my GrantApplication class called GrantApplicationStateId?
You must create another property:
public class GrantApplication
{
public int Id { get; set; }
...
public int GrantApplicationStateId { get; set; }
[NotMapped] // Perhaps not need
public GrantApplicationState GrantApplicationState
{
get { return (GrantApplicationState)GrantApplicationStateId; }
set { GrantApplicationStateId = (int)value; }
}
}
EFv4.1 doesn't support enums at all - you cannot map them. This will change in EFv4.2.
Still EF not support for Enums.. it will be on EF 5.0..check my try on here
http://the--semicolon.blogspot.com/p/handling-enum-in-code-first-entity.html