Choosing DB context when adding identity to existing project - c#

I'm trying to add Identity to an existing and working MVC project.
When used this command Add-Migration CreateIdentitySchema I got error message: 'More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.' and I chose the existing DB context that I have previously created for the main functionality of the application and now it shows 'There is already an object named 'ExpenseReport' in the database.' so it's trying to recreate my initial model.
Should I have a separate DB Context for Identity and if yes how to connect user to data from the other context?

So, i think you can use several DbContexts with different connections (just need to write it in your Startup class) or you need to use pattern "DbContext Factory". You can read more here .
Hope it will help you.

Related

How to add new table in sql server

I already created a Asp.net core web api project and have created a database using identity with migration. Now I need to know, that can I add new table to the database and how can i do that? I'm new to this field.
I also have tried Add-Migration but giving me error. I have no idea about this.
The error message is:
An error occurred while accessing the IWebHost on class 'Program'.
Continuing without the application service provider. Error:
AddEntityFrameworkStores can only be called with a user that derives
from IdentityUser. More than one DbContext was found. Specify
which one to use. Use the '-Context' parameter for PowerShell commands
and the '--context' parameter for dotnet commands.
Create your model and add in DB context.
public DbSet<UserAttachment> UserAttachment {get; set;}
After that
add-migration
And then
update-database

How do I resolve my Invalid object name 'dbo.AspNetUsers' error?

I changed my connection string when starting a new mvc project because I needed to use some info out of an existing database. Now I get a "Invalid object name 'dbo.AspNetUsers' when I try to load the startup page. A quick google search says the issue happens because you are connecting to an existing database using asp.net users. Still, this should not occur since I am not using code first migrations, right? What is more, I am using Active Directory to authenticate my users instead of individual user accounts.
In addition, the AspNetUserstable does not exist in the database I am calling in the web.config:
Due to the fact that I am not using the UserStore or the ApplicationUser provided by the Individual User Accounts defaults I needed to change this:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
to
public class ApplicationDbContext : DbContext
Optionally If you are using Code First Migrations you could add the initial migrations, remove all of the sql code from the up and down methods, and finally update the database. Even though the tables will still not exist it seems to satisfy the compiler and your project as the error goes away and the page loads as expected.

How to get lightswitch table data in mvc controller?

I am new and beginner in light-Switch,
I am doing light-Switch with MVC.
Let me explain my scenario:
First I have created one light switch application. In this app having one project is called [ProjectName].Server
Then I've Connect my SQL database in Light-Switch application. And try CRUD operation via Light-Switch screen and its working fine.
In This server project I've create MVC structure like controller, model and views. using Click Here
Then I've create simple Index view in Home controller and its open successfully.(via open browser from desktop client)
Now i want to get light-Switch table data(records) in my controller via entity Framework or light-Switch data context. suggest me easiest way..
I don't know how to get data in controller using light-switch data context. so I've try with entity framework using DbContext and DbSet.
But i got error:
One or more validation errors were detected during model generation:
LightSwitchApplication.Data.[EntityName]: EntityType '[EntityName]' has no key defined. Define the key for this EntityType.
[EntityName]: EntityType: EntitySet '[EntityName]' is based on type '[EntityName]' that has no keys defined.
I've also got some solution for light-switch data context here Click Here
I've try to implement but not getting ApplicationData and CreateContext See Attached.
Your answer will appreciable
Thanks,
Jatin
See my article: An HTML MVC LightSwitch Security Administration (http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/3281/An-HTML-MVC-LightSwitch-Security-Administration.aspx) for an example.
Basically you need to have these includes:
using Microsoft.LightSwitch.Server;
using Microsoft.LightSwitch;
using LightSwitchApplication.Models;

DNN Database Repository Reuse in Console Application

I have a project based on the Chris Hammond, Christoc, module template. I have a ton of code that I use to access data an external database. In my repositories I change the database from the default to whichever I need for that particular object. I do so with code that looks like this:
using (IDataContext ctx = DataContext.Instance(MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY))
{
var rep = ctx.GetRepository<Product>();
products = rep.Get().ToList();
}
The default database is switched in the call to .Instance(). The repositories are used by my custom DNN modules. The repository is part of the solution that contains multiple custom modules. When I compile and install using the Extensions part of DNN, everything works well. In the code above, MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY is found in a file MyModuleSettingsBase.cs file of my module solution. It is set to a simple string like "ProductDatabase". In the solution for the base DNN install (not the module solution), within the web.config file, there is a value in <connectionStrings> with name="ProductDatabase" which contains the actual connection string. This all links up fine on the DNN website.
Now I am writing a console application that does some monitoring of the site. I want to access the database to check values in the product table. I would like to reuse all of the repository code I have written. In an attempt to do so, I added a reference to the MyModules.dll file so I would only have one copy of the base code. This works to give me access to all the objects and the associated repositories but when I attempt to query data it fails. When debugging I can see that it fails on the line:
using (IDataContext ctx = DataContext.Instance(MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY))
When viewed in a debugger, the string value MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY is correctly set to "ProductDatabase" but the code is unable to link this with the actual connection string. I don't know where it would be checking for the connections string when running from my console application. I attempted to put a <connectionStrings> section into my App.config file but this didn't do the trick.
Is it possible to have MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY map to the connection string in an external application which references the DLL?
If so, where can I set the value of my connection string so it matches up to the key value stored in MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY?
I was faced similar problem 3 months ago, at that time I want to use DNN core libraries in my console application but I was failed.
I placed my queries in DNN official forum website and I got a valid response from Wes Tatters (DNN MVP).
Here is the post link: Reference URL
As your requirement of monitoring, I suggest you to create DNN Schedule Application. You can schedule it within DNN (Host->AdvancedSettings->Schedule), even good point is that you can use your repositories (DNN Libraries) in that schedule application.
I hope it solved your problem. Let me know if you have any questions.

C# ASP.net MVC database questions

I am trying to develop a website with C# ASP.net MVC. It's my first time using Visual Studio, C# and ASP.net so I have lots to learn but so far so good.
So far... I started a C# ASP.net MVC project and added a database by going to the Database Explorer and clicking "add connection". I then did some standard CRUD controllers and views.
I am at the stage where I want to implement User authentication. I'm a bit confused here. I am trying to make a custom Membership Provider. So I added it to my web.config file with the correct connection string etc.
When I run the project and go to register I get an error. "Could not find stored procedure 'dbo.aspnet_CheckSchemaVersion'."
From searching, I see lots of people have this problem and they always reference their hosting. People say this (http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx) is their solution but when I try pick a database I get an error. Not even sure of my server name.
So at this point I am wondering, did I set up the database right?
EDIT
Adding in a few pics to show exactly what I am doing. This is the aspnet_regsql.exe:
This is the provider with connection string, taken from an example on one of the links given.
This is my customized provider with connection string pointing to the last image.
This is a screen cap when I run the project and go to the default project Account register action:
and finally, this is the error screen when I submit
EDIT
Another update..
I sorted something out but I am not sure if it is correct. I am now getting an error when the page loads: "Invalid object name 'dbo.Tag'"
In order to solve this problem the only thing you need to do is create an application services DB. You can achieve this by running the following command from your Visual Studio Command Prompt
aspnet_regsql
Anyways it seems that your "custom provider" isn't using a custom structure for your DB, which might be the reason why you weren't expecting this error.
If you are using the default implementation, you can access to the user administration using ASP .NET Configuration, located on your project menu in visual studio.
The default implementation uses the following conn string keyword
LocalSqlServer
The are many ways of implementing the membership provider. My guess is that probably this conn string is not pointing to your aspnet services db, you could do something like this to specify a custom location for this db
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnetdb.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
I made a blog post regarding this topic: How to use MembershipRole Provider and when to use custom implementations
It's hard to figure out anything from your post.
but when I try pick a database I get
an error.
You can check your server name in Surface area configuration or Sql Server Configuration Manager. If you installed Visual Studio it's probably YOUR_MACHINE_NAME\SQLEXPRESS. You can connect to it using Windows Authentication. You could also write localhost,1433 instead, but that would require enabling TCP/IP first(it's disabled by default) and setting the port number first(which in most cases is already set).

Categories

Resources