Dynamic Data Entities Web Application Handle delete statement conflicted - asp.net

I've created an ASP.NET Dynamic Data Entities Web Application project with visual studio 2010.
I've added a ADO.NET Entity Data Model connected to a sql Server database.
The application works fine.
I'd like to handle an exception when deleting a row in a table that has a column which is a foreign key for another table.
The exception is :
The DELETE statement conflicted with the REFERENCE constraint "FK_name". The conflict
occurred in database "NAME", table "dbo.dbname", column 'Column_name'.
What I'd like to do is display a user friendly message to explain that the operation cannot be made before other rows in other table are delete.
I did some step by step debugging, but I can't find where the application does the database request, so that I can customize the code.
Thanks.

I did some research and i found two options :
- I can handle the delete directly with gridview attribute OnRowDeleting. This mean a lot of entity manipulation
- I can handle entity framework SavingChanges and check for entities on EntityState.Deleted, then check entity navigation properties and throw an explicit message if necessary. This works with a custom validation error to show user friendly message.

Related

Asp.net Register Stored Procedure gives error

I am using asp.net 4.6.1 and am unable to create an account via Register Page.
The stored procedure aspnet_Membership_CreateUser gives the following error:
The conversion of a datetime data type to a smalldatetime data type resulted in an out-of-range value.
The statement has been terminated.
I have tried googling it but have failed to find the solution to it.
The same functionality works fine locally creating an account on my local Sql db.
But does not create the account on server db.
Yep Buddy! Got that Fixed!
I was right about the aspnet_membership table.
Instead of importing membership and identity records, I had to create new entries in membership tables.
This time the Register page was able to create the User Account.
Thanks for being patient though, buddy..
Jamshaid Kamran, you kept my juices running.

Entity Framework Core Scaffold - Dynamic Schema Name

I'm having an issue at the moment where, we have a database which was already created, so used ef scaffold to create a model of it - the schema the model was created against is called "xxxx-dev".
Now, this has been fine, but in preparation to go live, I created a new DB server and provisioned the database to be called "xxxx-live". Switched the connection string, and attempted a query against it, and got an error.
It seems that scaffolding has hard-coded the schema name into every table in the OnModelCreating call, for example:
modelBuilder.Entity<xxxx>(entity =>
{
...
entity.ToTable("xxxx", "xxxx-dev");
...
});
This is a bit of an issue as going forward, we might have multi-tenant sites based on the same database, and obviously the query overriding the connection string every query isn't a great experience.
Is there anyway to configure the schema name, either in the Scaffold, or at runtime? I've done a bit of searching around and can't seem to see a solution.
Thanks,

Remove asp.net MVC Entity Framework auto-generated Global Uniquie Identifiers?

I just noticed that in the database for the auto generated ids for the default authentication users table the id is not an integer, it is a very large string.b(GUID)
here is an example:
a202fc44-7319-499e-80b8-96822f5833c0
Now all of the delete methods are implemented expecting an integer id and now obviously when it passes through its id. It gives an error.
1> why is it doing the string ids in he first place?
Is there any benefits in using this for a small scale application?
2> How can I change this to just use a normal auto-incremented int?
It is using code first migrations as it is implemented when I create the project. It just creates the database on the local sql server.

How do you force the OAuth Sql Server database objects to be recreated once deleted?

Using ASP.net 4.5, I created a membership database and set up some open auth providers. In the course of adjusting the database, I deleted the open auth tables (UsersOpenAuthAccounts and UsersOpenAuthData) - I can find no script to generate these items or any other way to regenerate them.
The ASP.Net membership libraries create a system table called __MigrationHistory. When these OAuth elements are created, it creates this table (if missing) and creates a record there which acts as a flag indicating that it is not necessary to create the db items. Delete the table, and the membership libraries will recreate the elements.
You will receive an exception indicating that the __MigrationHistory table is missing - ignore this error, as it will be recreated along with the other elements.
BUT BEWARE - __MigrationHistory is a table used by Entity Framework...you may jack it up in try to fix this, so keep a backup handy.

Entity framework activates trigger to other database: error

I have a ASP.NET web application which does some changes on a table in SQL Server 2008 R2.
On this table there is a trigger that does updates on another table in another database on the same database server.
When saving the changes I'm having the following error:
- The error message: The underlying provider failed on Commit.
- InnerException: This SqlTransaction has completed; it is no longer usable.
Also allowing the database user to connect to the other database isn't working.
Somebody knows how I can make this work?
The trigger is not related to Entity Framework.
The trigger fires when changes are made to the table irrespective of where that change came from.
This is probably a rights issue, the system is attempting to make the change to the second database with the security context that was used to connect to the first database. It the change that was caused by the trigger fails, then everything in the same transaction will fail.
Since you are accessing 2 databases in a transaction, you are using MSDTC, make sure that it is started and that you have rights to it.

Resources