I am hosting a .Net Web Api on azure, the api is just updating one row in a sql server database. Here is my Updatecode:
using (Storage ctx = new Storage())
{
string json = JsonConvert.SerializeObject(ev, _jsonsettings);
Data ex = ctx.dbsData.FirstOrDefault();
if (ex == null)
ctx.dbsData.Add(new Data() { Json = json, LastUpdate = DateTime.Now });
else
{
ex.Json = json;
ex.LastUpdate = DateTime.Now;
ctx.Entry(ex).State = EntityState.Modified;
}
ctx.SaveChanges();
return new HttpResponseMessage(HttpStatusCode.OK);
}
and i have a second method which only clears the table:
[HttpDelete]
public HttpResponseMessage Clear()
{
using (Storage ctx = new Storage())
{
ctx.dbsData.RemoveRange(ctx.dbsData);
ctx.SaveChanges();
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
See also my DbContext Class:
public class Storage : DbContext
{
public DbSet<Data> dbsData { get; set; }
public Storage(string connectionstring) : base(connectionstring)
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
Configuration.ValidateOnSaveEnabled = false;
Configuration.AutoDetectChangesEnabled = false;
}
public Storage() : this("tom")
{
}
}
My Problem is, that both methods are extremly slow. I found out if i remove the SaveChanges() Command it executes way faster. So my question is how can i improve performance and why does it take that long to update/delete one single row?
It totally depend on how much data is in your table ctx.dbsData.
ctx.dbsData.RemoveRange(ctx.dbsData); marks those many rows for deletion only. But changes will only reflect after calling SaveChanges();
For faster processing you can have async calls.
Related
A lot of code examples use either named parameters or execute stored procedures, but not both. How do I do so when I don't have a pre-defined entity type being selected by the stored proc? (Which means that .FromSqlRaw is out.)
The code below allows you to call a stored procedure and generate a list of named parameters, just from the list of SqlParameters.
var sqlParams = new SqlParameter[] {
new SqlParameter("p1", valOfP1),
new SqlParameter("p2", valOfP2),
new SqlParameter("pOut", SqlDbType.Int)
{
Direction = System.Data.ParameterDirection.Output
}
};
// OK to use the same names as for the SqlParameter identifiers. Does not interfere.
var sql = "myStoredProc " + String.Join(", ", sqlParams.Select(x =>
$"#{x.ParameterName} = #{x.ParameterName}" +
(x.Direction == ParameterDirection.Output ? " OUT" : "")
));
myDbContext.Database.ExecuteSqlRaw(sql, sqlParams);
var outputId = (int)(sqlParams.First(p => p.Direction == ParameterDirection.Output).Value);
Try Below example code which lets you call sp and store result in a
datatable.
using (var command = db.Database.GetDbConnection().CreateCommand())
{
command.CommandText = "sp_name";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("key", "Value"));
db.Database.OpenConnection();
using (var result = command.ExecuteReader())
{
var dataTable = new DataTable();
dataTable.Load(result);
return dataTable;
}
}
Here Product is class where you can define property whatever you want to retrieve from procedure
public class DataBaseContext : DbContext
{
public DataBaseContext() : base("name=DataBaseContext")
{
}
public DbSet<Product> Products { get; set; }
}
-- // This below code you need to write where you want to execute
var context = new DataBaseContext();
var products = context.Database.SqlQuery<Product>("EXEC GetProductsList #ProductId",
new SqlParameter("#ProductId", "1")
).ToList();
Add DbSet as below code
public DbSet ChartModels { get; set; }
Set Dbset AS a HasNoKey() if it is use only for Query
builder.Entity< ChartModel >().HasNoKey();
Call Sp as below Code
string sqlQuery = "EXECUTE dbo.GetDashboardChart";
SqlParameter p = new SqlParameter("#name", "test");
var lst = await ChartModels.FromSqlRaw(sqlQuery,p).ToListAsync();
Pretty much the same as SAEED said above. Add the code below to your DbContext class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Berk>().HasNoKey();
}
[NotMapped]
public DbSet<Berk> Berks { get; set; }
public virtual List<Berk> CallSP(string berkberk) =>
Berks.FromSqlRaw("exec dbo.berk #berkBerk = {0}", berkberk).ToList();
called with:
List<Berk> berk = = _whateverYouCalledTheDbContext.CallSP("berk berk berk!");
Will return a DbSet where Berk is just an object that matches the return values from the stored procedure. There is no Berks table in the database, but you have your stored procedure return values to play with as you wish.
I'm trying to implement item versioning using EF, and what I need to know is whether or not the entity which I called Update() on actually got changed, so I can increment its version number. How can I obtain this information?
My repository Update function looks like this:
public virtual void Update(T entity)
{
dbset.Attach(entity);
dataContext.Entry(entity).State = EntityState.Modified;
}
What I opted for afterall was comparing the serialized versions of the 2:
public void UpdateProduct(Product product)
{
var productInDb = GetByID(product.Id);
if (!JToken.DeepEquals(JsonConvert.SerializeObject(product), JsonConvert.SerializeObject(productInDb)))
product.CurrentVersion++;
product = Update(product);
}
You can get the information from the entity state.
If a property has been changed, the state will be "Modified" otherwise "Unchanged".
using (var ctx = new TestContext())
{
var first = ctx.Entity_Basics.First();
var x1 = ctx.IsModified(first); // false
first.ColumnInt = 9999;
var x2 = ctx.IsModified(first); // true
}
public static class Extensions
{
public static bool IsModified <T>(this DbContext context, T entity) where T : class
{
return context.Entry(entity).State == EntityState.Modified;
}
}
So the dynamic proxy is created, but I can't figure out what I've done wrong to prevent navigation properties from lazy loading. Here is the exact code I've run to test the issue.
DbContext:
public class MyDbContext : DbContext
{
public MyDbContext()
: base("MyConnection")
{
}
public DbSet<One> Ones { get; set; }
public DbSet<Many> Manies { get; set; }
}
Classes:
public class One
{
public int Id { get; set; }
public virtual ICollection<Many> Manies { get; set; }
public One()
{
Manies = new List<Many>();
}
}
public class Many
{
public int Id { get; set; }
public string Value { get; set; }
public int OneId { get; set; }
public virtual One One { get; set; }
public Many()
{
}
}
Test:
[TestMethod]
public void OneToManyTest()
{
One parent1 = new One();
parent1.Manies.Add(new Many() { Value = "child 1" });
parent1.Manies.Add(new Many() { Value = "child 2" });
using (MyDbContext db = new MyDbContext())
{
db.Ones.Add(parent1);
db.SaveChanges();
}
Assert.IsTrue(parent1.Id > 0, "Id not set");
One parent2;
using (MyDbContext db = new MyDbContext())
{
db.Configuration.ProxyCreationEnabled = true;
db.Configuration.LazyLoadingEnabled = true;
parent2 = db.Ones.Find(parent1.Id);//parent2 is a dynamic proxy
}
Assert.AreEqual(parent1.Id, parent2.Id);
/*parent2.Manies is null*/
Assert.AreEqual(parent1.Manies.Count, parent2.Manies.Count);//fails
}
Database:
I've verified the correct information is being inserted in the database. The relationships look good. I'm sure I'm missing something obvious.
Update
This works:
using (MyDbContext db = new MyDbContext())
{
db.Configuration.ProxyCreationEnabled = true;
db.Configuration.LazyLoadingEnabled = true;
parent2 = db.Ones.Find(parent1.Id);//parent2 is a dynamic proxy
Assert.AreEqual(parent1.Id, parent2.Id);
Assert.AreEqual(parent1.Manies.Count, parent2.Manies.Count);
}
This doesn't:
using (MyDbContext db = new MyDbContext())
{
db.Configuration.ProxyCreationEnabled = true;
db.Configuration.LazyLoadingEnabled = true;
parent2 = db.Ones.Find(parent1.Id);//parent2 is a dynamic proxy
}
using (MyDbContext db = new MyDbContext())
{
Assert.AreEqual(parent1.Id, parent2.Id);
Assert.AreEqual(parent1.Manies.Count, parent2.Manies.Count);//parent2.Manies is null
}
So the same db context is required for built in lazy loading.
To trigger lazy loading you need to access the property in some way, before disposing of the context.
Your test code doesn't acces the property before leaving the context:
One parent2;
using (MyDbContext db = new MyDbContext())
{
db.Configuration.ProxyCreationEnabled = true;
db.Configuration.LazyLoadingEnabled = true;
parent2 = db.Ones.Find(parent1.Id);//parent2 is a dynamic proxy
}
// Context disposed: thsi would throw an exception:
var manies = parent2.Manies.ToList()
At this point, your context has been disposed. If you tried to access the Manies property you'd get an error stating this.
One parent2;
using (MyDbContext db = new MyDbContext())
{
db.Configuration.ProxyCreationEnabled = true;
db.Configuration.LazyLoadingEnabled = true;
parent2 = db.Ones.Find(parent1.Id);//parent2 is a dynamic proxy
// Context available: this sill lazy load the Manies entities
var manies = parent2.Manies.ToList();
}
Now, if you check the manies properties, it will be available.
The idea of lazy loading is that, while the context is available, the first time you access a property which wasn't loaded initially, it will be loaded at that moment.
Please, see this article to understand the different ways (eager, lazy, explicit) of loading entities with EF:
Loading Related Entities
parent2 = db.Ones.Include(o=>o.Manies).FirstOrDefault(o=>o.Id == parent1.Id);
I have an MVC application that uses Entity Framework 5. In few places I have a code that creates or updates the entities and then have to perform some kind of operations on the updated data. Some of those operations require accessing navigation properties and I can't get them to refresh.
Here's the example (simplified code that I have)
Models
class User : Model
{
public Guid Id { get; set; }
public string Name { get; set; }
}
class Car : Model
{
public Guid Id { get; set; }
public Guid DriverId { get; set; }
public virtual User Driver { get; set; }
[NotMapped]
public string DriverName
{
get { return this.Driver.Name; }
}
}
Controller
public CarController
{
public Create()
{
return this.View();
}
[HttpPost]
public Create(Car car)
{
if (this.ModelState.IsValid)
{
this.Context.Cars.Create(booking);
this.Context.SaveChanges();
// here I need to access some of the resolved nav properties
var test = booking.DriverName;
}
// error handling (I'm removing it in the example as it's not important)
}
}
The example above is for the Create method but I also have the same problem with Update method which is very similar it just takes the object from the context in GET action and stores it using Update method in POST action.
public virtual void Create(TObject obj)
{
return this.DbSet.Add(obj);
}
public virtual void Update(TObject obj)
{
var currentEntry = this.DbSet.Find(obj.Id);
this.Context.Entry(currentEntry).CurrentValues.SetValues(obj);
currentEntry.LastModifiedDate = DateTime.Now;
}
Now I've tried several different approaches that I googled or found on stack but nothing seems to be working for me.
In my latest attempt I've tried forcing a reload after calling SaveChanges method and requerying the data from the database. Here's what I've done.
I've ovewrite the SaveChanges method to refresh object context immediately after save
public int SaveChanges()
{
var rowsNumber = this.Context.SaveChanges();
var objectContext = ((IObjectContextAdapter)this.Context).ObjectContext;
objectContext.Refresh(RefreshMode.StoreWins, this.Context.Bookings);
return rowsNumber;
}
I've tried getting the updated object data by adding this line of code immediately after SaveChanges call in my HTTP Create and Update actions:
car = this.Context.Cars.Find(car.Id);
Unfortunately the navigation property is still null. How can I properly refresh the DbContext immediately after modifying the data?
EDIT
I forgot to originally mention that I know a workaround but it's ugly and I don't like it. Whenever I use navigation property I can check if it's null and if it is I can manually create new DbContext and update the data. But I'd really like to avoid hacks like this.
class Car : Model
{
[NotMapped]
public string DriverName
{
get
{
if (this.Driver == null)
{
using (var context = new DbContext())
{
this.Driver = this.context.Users.Find(this.DriverId);
}
}
return this.Driver.Name;
}
}
}
The problem is probably due to the fact that the item you are adding to the context is not a proxy with all of the necessary components for lazy loading. Even after calling SaveChanges() the item will not be converted into a proxied instance.
I suggest you try using the DbSet.Create() method and copy across all the values from the entity that you receive over the wire:
public virtual TObject Create(TObject obj)
{
var newEntry = this.DbSet.Create();
this.Context.Entry(newEntry).CurrentValues.SetValues(obj);
return newEntry;
}
UPDATE
If SetValues() is giving an issue then I suggest you try automapper to transfer the data from the passed in entity to the created proxy before Adding the new proxy instance to the DbSet. Something like this:
private bool mapCreated = false;
public virtual TObject Create(TObject obj)
{
var newEntry = this.DbSet.Create();
if (!mapCreated)
{
Mapper.CreateMap(obj.GetType(), newEntry.GetType());
mapCreated = true;
}
newEntry = Mapper.Map(obj, newEntry);
this.DbSet.Add(newEntry;
return newEntry;
}
I use next workaround: detach entity and load again
public T Reload<T>(T entity) where T : class, IEntityId
{
((IObjectContextAdapter)_dbContext).ObjectContext.Detach(entity);
return _dbContext.Set<T>().FirstOrDefault(x => x.Id == entity.Id);
}
I have a problem with something that seems to be a bug in Entity Framework 4.1: I have added a handler on ObjectContext.SavingChanges which updates a property "LastModified" whenever an object is added to or modified in the database. Then I do the following:
Add two objects to the database, and submit (call SaveChanges())
Modify the first object that was added
Extract the two objects ordered by LastModified
The resulting objects are returned in the wrong order. Looking at the objects, I can see that the LastModified property has been updated. In other words, the SavingChanges event was fired properly. But looking in the database, the LastModified column has not been changed. That is, there is now a difference between EF's cached objects and the rows in the database.
I tried performing the same update to LastModified in an overridden "SaveChanges" method:
public override int SaveChanges()
{
SaveChangesHandler();//updating LastModified property on all objects
return base.SaveChanges();
}
Doing this caused the database to be updated properly and the queries returned the objects in proper order.
Here is an entire test program showing the error:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Reflection;
using System.Threading;
namespace TestApplication
{
class Program
{
private PersistenceContext context;
private static void Main(string[] args)
{
var program = new Program();
program.Test();
}
public void Test()
{
SetUpDatabase();
var order1 = new Order {Name = "Order1"};
context.Orders.Add(order1);
var order2 = new Order {Name = "Order2"};
context.Orders.Add(order2);
context.SaveChanges();
Thread.Sleep(1000);
order1 = GetOrder(order1.Id); // Modified 1.
order1.Name = "modified order1";
context.SaveChanges();
List<Order> orders = GetOldestOrders(1);
AssertEquals(orders.First().Id, order2.Id);//works fine - this was the oldest object from the beginning
Thread.Sleep(1000);
order2 = GetOrder(order2.Id); // Modified 2.
order2.Name = "modified order2";
context.SaveChanges();
orders = GetOldestOrders(1);
AssertEquals(orders.First().Id, order1.Id);//FAILS - proves that the database is not updated with timestamps
}
private void AssertEquals(long id1, long id2)
{
if (id1 != id2) throw new Exception(id1 + " != " + id2);
}
private Order GetOrder(long id)
{
return context.Orders.Find(id);
}
public List<Order> GetOldestOrders(int max)
{
return context.Orders.OrderBy(order => order.LastModified).Take(max).ToList();
}
public void SetUpDatabase()
{
//Strategy for always recreating the DB every time the app is run.
var dropCreateDatabaseAlways = new DropCreateDatabaseAlways<PersistenceContext>();
context = new PersistenceContext();
dropCreateDatabaseAlways.InitializeDatabase(context);
}
}
////////////////////////////////////////////////
public class Order
{
public virtual long Id { get; set; }
public virtual DateTimeOffset LastModified { get; set; }
public virtual string Name { get; set; }
}
////////////////////////////////////////////////
public class PersistenceContext : DbContext
{
public DbSet<Order> Orders { get; set; }
public PersistenceContext()
{
Init();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
public void Init()
{
((IObjectContextAdapter) this).ObjectContext.SavingChanges += SavingChangesHandler;
Configuration.LazyLoadingEnabled = true;
}
private void SavingChangesHandler(object sender, EventArgs e)
{
DateTimeOffset now = DateTimeOffset.Now;
foreach (DbEntityEntry entry in ChangeTracker.Entries()
.Where(entity => entity.State == EntityState.Added || entity.State == EntityState.Modified))
{
SetModifiedDate(now, entry);
}
}
private static void SetModifiedDate(DateTimeOffset now, DbEntityEntry modifiedEntity)
{
if (modifiedEntity.Entity == null)
{
return;
}
PropertyInfo propertyInfo = modifiedEntity.Entity.GetType().GetProperty("LastModified");
if (propertyInfo != null)
{
propertyInfo.SetValue(modifiedEntity.Entity, now, null);
}
}
}
}
I should add that the SavingChanges handler worked fine before we upgraded to EF4.1 and using Code-First (that is, it worked in EF4.0 with model-first)
The question is: Have I found a bug here, or have I done something wrong?
I'm not sure if this can be considered a Bug. What seems to happen is that the way you manipulate the LastModified property does not trigger INotifyPropertyChanged and thus the changes do not get populated to your Database.
To prove it use:
order2.Name = "modified order2";
((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.GetObjectStateEntry(order2).SetModifiedProperty("LastModified");
To utilize this knowledge in your SavingChangesHandler:
private void SavingChangesHandler(object sender, EventArgs e)
{
DateTimeOffset now = DateTimeOffset.Now;
foreach (DbEntityEntry entry in ChangeTracker.Entries()
.Where(entity => entity.State == EntityState.Added || entity.State == EntityState.Modified))
{
SetModifiedDate(now, entry);
if (entry.State == EntityState.Modified)
{
((IObjectContextAdapter) this).ObjectContext.ObjectStateManager.GetObjectStateEntry(entry.Entity).SetModifiedProperty("LastModified");
}
}
}
Edit:
I looked into this a little more and you are correct. For some reason MS decided to not fire PropertyChanged events when using PropertyInfo.SetValue anymore. Only one way to find out if this is a bug or a design decision: File a bug report / Post to msdn Forums.
Though changing the property directly via CurrentValue seems to work fine:
private static void SetModifiedDate(DateTimeOffset now, DbEntityEntry modifiedEntity)
{
if (modifiedEntity.Entity == null)
{
return;
}
modifiedEntity.Property("LastModified").CurrentValue = now;
}