Not really a problem, but curiosity. Should a savepoint name in EF be unique? Since I thought each of the names refers to a transaction for each client.
using ShortId;
string uniqueID = ShortId.Generate();
transaction.CreateSavePoint($"Save_{uniqueID}");
I've searched for this case before but haven't found anything.
Should a savepoint be unique?
Related
Is there any way to find an entity with its unique id without knowing its type ?
For example write
DataContext.Find(Id);
instead of
DataContext.Set<T>().Find(Id);
Thanks
Id is unique only within the table. You can not give Id and ask, in which table is it used...You do not want to do it this way :)
I was wondering If it was always the case that saving an object to the db in ASP MVC updates the fields of the object?
Just for illustration say I have a Student class with fields ID (int) and Name where.
The key for the records is clearly ID
If I have a student: Student student = new Student("studentName");
and do
Context.Insert(student); ID will be assigned a number
Context.Save();
Is it always safe after such operation to assume that "student" ID has been updated? Or do I have to query it from the database.
Thanks
Well... It depends! :)
You are describing a case where EF conventions are doing some of the work for you.
Code First convention looks for fields that are named Id or [class name]Id and determines that these are meant to be keys for the classes. You class design simply match this convention.Because you Id property is integer type, EF has also configured it to be identity column in the database. So, when you save your object, your database generates the Id and you context is updated acordingly.
About documentation, Julia Lerman three books about EF are a must.
If you are only saving, yes, ID will be automatically incremented.
But if you are going to edit/delete a certain record, you need to query the id first before firing
Context.Save()
Whenever I want to get db table name from Entity Framework it concatenates Garbage value with it.
It only happens whenever I try to delete any entity
It's not table name. Its name of entity type. You have lazy-loading enabled, so you see the dynamic proxy class name. If you'll turn off proxy generation, then you'll see expected type name:
db.Configuration.ProxyCreationEnabled = false;
But I would not recommend treat this information as table name, because entity can be mapped to any table, which does not have same name as entity type.
Hi guys Im having problem with entity framework code first in a detached scenario.
I have two Entities Order and User.
the Order class has a property of type User
so Everytime I save an Order object I have to:
order.UserId = order.CurrentUser.Id;
order.CurrentUser = null;
Im assigning the UserId of type int for foreign key purpose
but I have to null the CurrentUser Property.
Do you guys have a better way to approach this issue?
Just use the UserId property not the Order class and don't use the Order.CurrentUser navigation property because you do nothing with it.
I'm using entity framework code first to create my tables. Please note - create the tables, not the DB, since I'm working on a hosted environment and I don't have a user that is allowed to create db's.
Committing a DB update works fine, but retrieving data gives the exception:
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.EventHosts'.
I've read that it happens because I'm not using EF Code First to create the DB. That's fine, but how do I elegantly solve this?
All the generated tables do not have a prefix like dbo. A solution like this doesn't work, and isn't elegant at all:
[Table("EventHosts", Schema = "")]
Ok, for me issue was that I had a table called dbo.UserState and in C# EF was trying to access dbo.UserStates because of pluralization.
The solution was to put Table attribute above class and specify the exact table name:
[Table("UserState")]
public class UserState
{
[Key]
public int UserId { get; set; }
}
To answer your first question: use the schema created for you by your hosting provider.
To answer your second question: No there is currently no direct way to change the default schema globally because you cannot modify existing conventions or create new conventions. You can try to hack it.
For example you can override OnModelCreating and use reflection to get all DbSet<> properties declared in your context. Than you can just use simple loop on these properties and create ToTable mapping call with name of the property as table name and your custom schema. It will require some playing with reflection to make this work.
Alternatively you can try to do some reusable approach by implementing custom conventions. You can find many different articles about using your own conventions with EF. Some examples:
Custom Conventions in Entity Framework Code First v 4.1
Conventions in Entity Framework 4.1 Final
My high level untested idea is following same principle and create assembly level attribute which will be processed by the convention mechanism and applied on all your entities.
Try to set default schema name to 'dbo' in SQL SERVER.
http://msdn.microsoft.com/en-us/library/ms173423.aspx
On of the reason for this error is the table named "EventHosts" may not Exist or that table is renamed to some other name please check with that..
https://stackoverflow.com/a/12808316/3069271
I had same issue, it was pluralize problem between mapping and db.