There are already many workarounds in this problem. And in this comment I will try to give a basic question why this might be unsuccessful. [Hoping to inform people of the root cause]
Say you have a DbContext (a specific, child class of DbContext) in your application, and you are trying to use a model class (albeit a model), as well as controllers and views of DbContext and forests.
I assume that DbContext did not have the DbSet & Model Model (get; set;} "property, but the DbSet was added to the DbContext using the code in the OnModelCreating method.
In the above case, the scaffolds first try to discover the DbSet property in the DbContext (only by reflection - so as not to detect if OnModelCreating has code to add the DbSet), and if it doesnβt, the scaffolding adds the DbSet property to the DbContext and then tries run a relay using this DbContext, however when we run scaffolding we create an instance of DbContext and we also call OnModelCreating, and at that moment scaffolding fails because there are several types of DbSet in DbContext with the same model (one forest added and one configured th code in OnModelCreating).
[This happens not only for the model used, but also for related models in this model, scaffolding adds DbSet properties for all related models]
[The added DbSet also does not appear after scaffolding has been done because scaffolding rolls back any changes if the operation did not complete successfully, as Jeff said, the error message was bad initially and was improved to give some hint to the user. but he is still not very clear what is going on]
This is a mistake in scaffolding, a simple job will be to use the DbSet property in DbContext for all related models of your model class instead of setting them in OnModelCreating.
pradeep Jun 02 '15 at 21:55 2015-06-02 21:55
source share