Model First scaffolding not working in ASP.NET MVC3 Application - asp.net-mvc-3

I am building a controller using scaffolding, from a database model.
The database in the backend is SQL Azure, although I am not sure if that matters.
I entered the connection string, and tested, and it works. I added a new ADO.Net Entity Data Model to my models, and it created the mappings, and I can view the table structure and the FKs when I open the created model, and it looks correct.
I built the solution so the new model is available.
I made sure I have the latest version of Entity Framework, and downloaded the MvcScaffolding from NuGet.
When I add new controller with read/write actions and views, using Entity Framework, I get the following error:
The type 'Website.Models.App.Application' was not mapped.
Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation.
Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.
When I try to add the same controller using MvcScaffolding: Controller with read/write actions and views, using EF data access code, I do not get an error at this step, but when I navigate to the Index View of the new controller I get the same error.
Any ideas about what is causing this error? I have seached long for a solution but everything is coming up empty. Thank you.

Install Net DbContext Generator, if not available from online templates in your Visual Studio 2010

in the entity designer (.edmx file), right click on a blank area, click 'Add Code Generation Item', select 'ADO.NET DbContext Generator' give it a name, click add. >>cant remember why you have to do this :-( but it fixed the same problem for me.

You probably nested your your classes in the main class:
static void Class main(string[] args)
{
public Class YourClass() // This is the wrong location, it's nested in the main class
{
...
}
}
public Class YourClass() // This is the correct location
{
...
}

Related

Is there a simple way to auto register [Exports] in a Prism app? WPF .NET 4.8

I used to be on a project that used Prism and when we needed a new service to do something, we'd just create an interface, a concrete class that implemented that interface and exported it, and then it just became available everywhere for [ImportingConstructor]. We didn't need to manually register it or anything. I no longer have access to that project, but I don't think there was any reflection magic that was done manually to accomplish this.
I'm in a new company and we are starting up a project using MEF / Prism and I'm trying to accomplish the same thing, but as of right now, I'm having to manually register items in order to import them. What am I missing?
I'm in .NET 4.8 WPF app
Additional info
we are basing our project from this website
https://prismlibrary.com/index.html
This is our app class
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<ShellWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IStartupActionService, StartupActionService>();
containerRegistry.RegisterSingleton<IGeneralNavigationService, GeneralNavigationService>();
containerRegistry.RegisterSingleton<IExperimentSetupNavigationService, ExperimentSetupNavigationService>();
containerRegistry.RegisterSingleton<IProtocolSetupNavigationService, ProtocolSetupNavigationService>();
containerRegistry.RegisterSingleton<IOurProjectNavigationService, OurProjectNavigationService>();
containerRegistry.RegisterSingleton<IOurProjectUiService, OurProjectUiService>();
containerRegistry.RegisterManySingleton<WcfClientService>();
containerRegistry.RegisterSingleton<IControlClientService, ControlClientService>();
}
}
Why do I have to register each new service?
I've been reading about MEF, DryIoc, and others and I'm just not getting clear answers. Is there not a way to just have everything with an [Export] become immediately available for import?
Something else I need to do, that I think this whole registering thing is messing me up on is trying to come up with a way to have "dialogs" but tie them to a neutral class to make it more MVVM happy.
Dialog -> a region that pops open when you call a method. This method currently takes in a UserControl, assumed to have already been constructed and its ViewModel datacontext already attached.
What I would like to do and don't know how to start is
use a neutral container class to open one of these dialogs (similar to interaction request Notification
using attributes, attach an attribute to a view that indicates "I support this neutral container class" (assumed only one view per container)
this view supports [ImportingConstuctor] to bring in its ViewModel
the viewmodel itself supports [ImportingConstuctor] to bring in services needed
again, the desire to NOT need to register these items manually as we add them. Would like to add a service interface, the concrete class that [Export]s the interface and have it just available to the viewmodel and other services, and same for the views and viewmodels, export attribute tag them as necessary and have them just available to either/both grab an instance of them or manually create an instance of them and have their [ImportingConstructors] handled for me.

Execute Code First Migrations is Grayed Out in Publish Settings

Using Windows Azure and attempting to publish my MVC3 Application. The check box for Execute Code First Migration in the settings panel of the Publish web application is grayed out. What changes do I need to make to be able to enable it?
I believe you see the following "Execute Code First Migration" disabled when you try to publish your MVC application:
This is potentially because either you do not full code written for Code migration in your application as well no or incorrect DB setup in your web.config as described here.
In order to have Code Migration enabled, you must have a DB configured (in case of Windows Azure you need to provide SQL Database info in the web.config) in web.config and a complete class is written on how the code migration will happen depend on your model. Here is an example on how to achieve it.
http://msdn.microsoft.com/en-us/library/dd394698#efcfmigrations
I am assuming that you have Entity Framework model and in your database already (if not then you need to do some reading, answer by #AvkashChauhan would be indeed a good starting point).
However if you do have a model and all the configurations like:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new YourEntityMap());
}
and all the entity mappings like:
public class YourEntityMap : EntityTypeConfiguration<YourEntity>
{
public YourEntityMap()
{
this.HasKey(t => t.Id);
}
}
and you still don't get the darn checkbox enabled you might want to do following steps:
Go to Tools > NuGet Package Manager > Package Manager Console
Then in console write
Enable-Migrations -ContextTypeName Company.Models.YourDevContext
where Company.Models.YourDevContext is your Database Context (look for class that inherits from DbContext should be same one that has OnModelCreating override).
after running command you should get something like:
At this point you should have Migrations folder added to the solution more on how to handle migrations here
Hope this saves you some time.

How to hide connectionStrings on Github for both Entity Framework and Membership, that will be used on AppHarbor later on?

I know the title looks somewhat confusing so let me explain. :)
I have a MVC 3.0 Project for which I maintain a public repository # github, and said Project utilizes the Entity Framework and the Membership Provider (so that's two separate connectionStrings) .
The issue I am having is that the "Alias" attribute of Sequilizer for the MS SQL database, let's me specify only one connectionString value to be replaced at build time. Because I can not modify web.config programmatically (and web.config transformations are not applicable because it will leave my sensitive data open to the World on the public github repo), I am left to choose which I want to use more -> Entity or Membership (the other solution would be leaving one of my connectionStrings vulnerable) .
Due to the need of MembershipProvider to know the connectionStringName at build time (everything is specified in web.config), I am kinda at a loss of how to go about all this.
Any help is appreciated!
Cheers
Okay guys, I think I set up everything finally.
What I did is, I used the Alias only for my normal connection string which will provide Membership stuff - while I overrode the ObjectContext default constructor (in "MyProjectEntities.Designer.cs") to use Configuration Variable (named "EntityFramework") that I set up on AppHarbor which holds the Entity Framework connectionString like so:
public MyProjectEntities() : base(ConfigurationManager.AppSettings["EntityFramework"].ToString(), "eTestHubEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
Tested and works as I wanted it to work. :)

Adding WCF proxy class hides Visual Studio project reference

I have a weird visual studio error which is bugging me.
I am using Visual Studio 2010
My solution is small and contains 2 projects:
Project 1 a class library contains the following classes:
Namespace1.DataClass (a serializable class to hold data)
Namespace2.AccessorClass (a class with a method to pull instances of Namespace1.DataClass from a database via Linq to Sql)
Project 2, a console application contains the following classes:
Namespace3.Program (with main function which pulls data from the database and sends it via a wcf service)
Namespace4.WCFProxy (a wcf proxy generated by svcutil)
The WCF client sends objects to the service in the form of:
[System.Runtime.Serialization.KnownTypeAttribute(typeof(Namespace1.DataClass))]
public class SendItem
{
public object Item { get; set; }
public string Label { get; set; }
}
(This is generated by svcutil, so I am paraphrsing to save space)
So, within my proxy class there is a reference to Namespace1.DataClass.
When building my application I first created the project to access the database, I then created my console app, I added a reference from my console app to my my class library and finally I generated my proxy and added it (un-edited) to the console app project.
Everything looks fine, no VS compile errors BEFORE building.
Then, when I build, VS seems to forget the reference from my console app project to my class library project and I get a heap of compile errors accordingly.
If I exclude my proxy class from the project, VS can see the reference again. If I re-add the proxy everything is still fine (no errors and full intellisense support) but click build and everything goes haywire again.
Has anyone come across this issue before?
Cheers
Shane
It was log4net, beware of this behavior in future.
Just to clarify, this problem had nothing to do with WCF as I though it did.

NSArrayController and referring to a shared, static, Core Data based library

Using this guide I have created a static library (let's call it AppCore) that can be shared between the Mac OS X and iOS versions of one app. This static library uses Core Data and the point of it is to share the model part and schema versioning between different implementations.
I created a NSPersistentDocument based project that will depend on this AppCore. In this project I added a reference to the .xcdatamodel file. Then I created a simple table view with add/remove buttons to edit an array of one entity type with the assisted "new core data entity" item. This created an instance of NSArrayController and the required bindings for the add/remove behaviour.
Now, everything seems to work fine when I'm using the default class for the Core Data entities (NSManagedObject) and I'm able to add new rows using the +/- buttons. However, when I change the entity implementation class to a custom one, I'm getting an error
Failed to create new object
This seems to come from the NSArrayController and it seems to be unable to instantiate the required entity. I can, however, create one in the NSPersistentDocument subclass by:
[NSEntityDescription insertNewObjectForEntityForName:#"SomeEntity" inManagedObjectContext:[self managedObjectContext]]
What confuses me is why the instance of NSArrayController can't. If I understand correctly, the array controller is instructed to create an entity, not class and my guess is that the entities are created with the help of NSEntityDescription class. I could implement my own version of the array controller's add: but then again, it might be that here something is fundamentally wrong. I haven't touched the init:s and the custom entity class implementation is simply for convenience, to access the attributes directly.
I have tried changing the base SDK on the AppCore but without effect. Currently it uses the iOS version but I'm not sure how it should be. This is another question but if unrelated, I might ask it here on a separate question.
So, to summarize, why can't the NSArrayController create an instance of this entity?
Thanks in advance.
Update
This works if I add the SomeEntity class from the AppCore to the dependent project as a reference. This is not the most usable way since modifications to the AppCore has to be propagated to the dependatnt projects also.
Bingo. I missed the "-ObjC" flag for the dependant project's "other linker flags". Now everything works like a charm.

Resources