I recently restructured my MVC 4 Application and now face the problem that the database is not being initialized anymore.
In Global.asax, I'm calling the initializer on first start this way:
Database.SetInitializer<MyContext>(new MyInitializer());
new MyContext().Portfolios.Find(1);
new MyContext().UserProfiles.Find(1);
MyInitializer contains a little seed method.
public class MyInitializer : CreateDatabaseIfNotExists<MyContext>
{
protected override void Seed(MyContext context)
{
...
Initializer and Context are in namespace .DAL which I reference to in Global.asax. The Database is empty and I would expect it to be created, but it doesn't.
It seems like the Initializer is not being called at all..it makes no difference if I set the Initializer to DropCreateDatabaseAlways and I don't get why. Does any of you have an idea or a good link?
I apologize for my question ... it did not contain the info that was necessary to answer it. When I said that I restructured my project, I should have written instead that I added lots of inheritance where formerly, there was no inheritance. Seems like in OnModelCreating, an entry modelBuilder.Entity(); is needed in order for MVC to consider that entity as included.
Related
How can I use DropCreateDatabaseIfModelChanges in a VB.NET application in an early development state where the model changes quite often. I know I have to add those lines to Global.asax - but I only found examples using C# - could someone please give me a hand?
You should define a class which will be used to initialise your database. I usually call mine DatabaseContextIntializer. This needs to inherit from DropCreateDatabaseIfModelChanges
The DatabaseContextIntializer class needs to implement the method Seed(YourDataContextClassNameHere context) and you can use this to populate your database when it is initially created.
Got it: global.asax.vb
Imports System.Data.Entity
...
Database.SetInitializer(New DropCreateDatabaseIfModelChanges(Of <ENTITYNAME>)())
Today i was making changes to my database model. I was modifying it for like 2 hours, without compiling it.
So, i have this in my application:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDb>());
And usually when i was making changes to my model, it was enough to do this:
var tmp = new MyDb();
tmp.Categories.Any();
And model was recreating itself. But now, after i changed my model, i am getting an error:
Object reference not set to an instance of an object.
I have totally no idea what does that mean, since i am invoking MyDb(), so this instance is set. What is going on here? How can i check what is wrong?
Edit: The thing i discovered just now, is that it's not even trying to recreate database. I have set this:
public class MyDatabaseInitializer : DropCreateDatabaseIfModelChanges<ScykDb>
{
protected override void Seed(ScykDb context)
{
base.Seed(context);
(code...)
}
}
Into my application_start:
Database.SetInitializer(new MyDatabaseInitializer());
And i put a breakpoint in this method, and it's not even hitting it!
I just ran into this and I had not before. It drove me nuts till I figured out what I was doing. If you have a model that has a List to another model, make sure to have a constructor on your model initialize the list.
Okay. For anyone who enters here, here's the solution:
I have no idea how that works, but i commented out ALL my [ForeignKey] attributes and ALL my fluent api configuration. I then started the project, and fixed errors that it was throwing by using ONLY fluent api. This helped. All works just fine now.
I'm new in Db4o
As far as I checked, when I do a refactoring of a class name or path (packages),
db4o doesnt recognizes the class anymore and creates a new 'category'.
I want to make an API, so that any class refactoring in my app won't affect the database.
I've created the following function ...
public void saveClassAs(Class objClass, String nameInDB){
configuration.common().objectClass(objClass).rename(nameInDB);
}
and have it called for each of my classes. Since the nameInDB will be constant for each class, no matter where the class is located or its name.
The problem is that Im not sure if will this work and I don't know if this is a good idea in matters of speed ... I didn't find any details on db4o documentation about how the renaming API works. Any help?
If you want to use constant class names (irrespective to the real classes names) my best bet is to use aliases.
Regarding renaming configuration you can find details here.
I have an application using the Entity Framework code first. My setup is that I have a core service which all other services inherit from. The core service contains the following code:
public static DatabaseContext db = new DatabaseContext();
public CoreService()
{
db.Database.Initialize(force: false);
}
Then, another class will inherit from CoreService and when it needs to query the database will just run some code such as:
db.Products.Where(blah => blah.IsEnabled);
However, I seem to be getting conflicting stories as to which is best.
Some people advise NOT to do what I'm doing.
Other people say that you should define the context for each class (rather than use a base class to instantiate it)
Others say that for EVERY database call, I should wrap it in a using block. I've never seen this in any of the examples from Microsoft.
Can anyone clarify?
I'm currently at a point where refactoring is possible and quite quick, so I'd like some general advice if possible.
You should wrap one context per web request. Hold it open for as long as you need it, then get rid of it when you are finished. That's what the using is for.
Do NOT wrap up your context in a Singleton. That is not a good idea.
If you are working with clients like WinForms then I think you would wrap the context around each form but that's not my area.
Also, make sure you know when you are going to be actually executing against your datasource so you don't end up enumerating multiple times when you might only need to do so once to work with the results.
Lastly, you have seen this practice from MS as lots of the ADO stuff supports being wrapped in a using but hardly anyone realises this.
I suggest to use design principle "prefer composition over inheritance".
You can have the reference of the database context in your base class.
Implement a singleton for getting the DataContext and assign the datacontext to this reference.
The conflicts you get are not related to sharing the context between classes but are caused by the static declaration of your context. If you make the context an instance field of your service class, so that every service instance gets its own context, there should be no issues.
The using pattern you mention is not required but instead you should make sure that context.Dispose() is called at the service disposal.
It doesn't seem like ControllerActionInvoker has any implementation details that require a new instance to be created for each Controller. It seems to have two properties with setters that are never used, and getters that are basically lazy references to static members.
I am considering changing the scope of my custom ControllerActionInvoker's life cycle in my ASP.NET MVC application. Is there a good reason I shouldn't do this? Is there something I'm missing about this class?
There isn't anything implicitly wrong with implementing the IActionInvoker this way.
However, there is also no implicit benefit. It depends on how you want to scope that particular component of the MVC lifecycle.