SimpleMembership provider & Foreign key contraint exception? - c#

I am trying to understand the SimpleMembershipProvider in detail. So far, I have created a new project in VWD-2012 with an empty Asp.Net MVC - 4 template.
I have been following this great tutorial. I was able to just copy and paste all the classes from this tutorial into my project. I then used the following line of code in my global.asax Application_Start method to initialize the SimpleMembershipProcider :
WebSecurity.InitializeDatabaseConnection("dbcontext", "UserProfile", "Id", "Name", true);
(I am using Id and Name as a column name in my userprofile table)
After that I enabled migration in my project, created the initial migration and then Update-Database. Then I check my database in visual studio and all classes related to SimipleMembership were formed.
Then I create a user and successfully logged in. Up to this point everything was working fine. I then added Facebook as an identity provider on my website. In this process when the code line
OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
trys to create an entry in webpages_OAuthMembership table. However the following exception was raised:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.webpages_OAuthMembership_dbo.webpages_Membership_UserId". The conflict occurred in database "dbcontext", table "dbo.webpages_Membership", column 'UserId'.
The statement has been terminated.
Can anybody please help me solve this issue?

Related

I am trying to update a record in a table using Entity Framework Core but I am getting an exception

I am trying to update a record in a table using Entity Framework Core in ASP.NET Core MVC, but I am getting an exception while calling
context.SaveChanges()
saying that record is not there or deleted.
But that record with same id is there in table. I checked it with debugging.
I am suspecting you are referring to the wrong database.
I have encounter this problem before and the problem is that while I created the connection to the database to generate the model, VS let me choose whether to include the .mdf in the solution and change the connection string.
Please double check your connection string is pointing to the right database.

Asp.net Identity User, Adding First Name and Last Name

I have used this link to add first name and last name. Now I'm seeing an error Cannot drop database "dbname" because it is currently in use..
I don't want to drop my Database, just want to add first name and last name in register of asp.net identity user in mvc5. give me suggestions..
DropCreateDatabaseAlways I was using this option. After changing to
Database.SetInitializer<ApplicationDbContext>(new
MigrateDatabaseToLatestVersion<ApplicationDbContext,
APPLICATION.Migrations.Configuration>());
This works fine, also I need to enable migration using package manager console
Enable-Migrations
Add-Migration "FirststName"
Add-Migration "LastName"
Thanks #Archana Parmar

Visual Studio not creating edmx file using Entity Framework 6.1

Im trying to use Entity Framework on my .NET 4.0 Web Site. I already have installed Entity Framework 6.1 and i "Add New" ADO.NET Entity Data Model to my site using a valid connection string , and I select my table. I know this is a valid table with several columns and rows of data. After the creation is finished I am left with only a .cs file with no properties and a new connection string added to the web.config, but there is no .edmx file created. I know the SQL user in the Con String has read/write permissions and this is a valid table. Any clue as to what i may be doing wrong?
Have you tried to change the values of this new connection string with your values?
Take a look of this link and retry..
https://msdn.microsoft.com/en-us/data/jj206878.aspx
PS: I think that you should have created a "Project" and not a "new WebSite" using:
"Folder>New>Project> ASP Web App" in VS
Doing this you have all ready to go (references, EF etc..) then you could even start from a empty virgin model, or EF designer from database where you could easly manage or create entities, relations etc.

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;

Adding Schema Name to SQL Table Names in ASP.NET Web Application

I am porting my SQL Server database to Azure SQL. The database has been replicated correctly and is now accessible from my existing web application.
However, the first time it was executed with the Azure SQL connection string, it showed "Invalid Object" for a table name used on the log in page.
I was able to correct it by adding the schema name [dbusername].[tablename] in place of [tablename] in the code.
Now since it is an extensive application, with over 300 ASPX pages and 30 tables, I'm wondering do I have to use the Find-Replace tool in Visual Studio or am I missing a setting that can help me to assign the default schema name while executing all queries ?
Set the default schema for your user.
USE MyDataBase;
GO
ALTER USER MyUser WITH DEFAULT_SCHEMA=MySchema;
GO
I believe this article will help you.
http://blog.sqlauthority.com/2008/12/26/sql-server-fix-error-msg-15151-level-16-state-1-line-2-cannot-alter-the-login-sa-because-it-does-not-exist-or-you-do-not-have-permission/

Categories

Resources