Changing PK-FK -> 'Identity System' ASP.NET --> with MySql Database - mysql

-->
I am using mysql database with:
Asp.net mvc project: 6.0 (Long-term support)
Authentication type: 'Individual accounts'
Question 1) I want to change the PK UserId to 'Email' in the scaffolded table: AspNetUsers.
I created a class extending IdentityUser and override email to change the PK. But it raises the issue PK-FK constraint in identity scaffolded tables (ex. AspNetClaims, AspNetRoles,...). What is the best way to solve the issue?
Might need to add something in DbContext - onModelCreating() method. Can anyone suggest how to configure it?
Question 2) Is using .net version 6.0 is compatible with my use case? (mysql database and changing PK-FK)? Or I should use .net 3.1?
Question 3) Should I use Userid (PK of scaffolded AspnetUsers table) in my other mysql tables to not get into any trouble of changing PK-FK constraints?
Question 4) Is there any way to indicate any changes required, and then application generates initial migration document for 'identity system schema'? To skip all this problem? Or Visual Studio should introduce something like that?
Question 5) What is your suggestion to code for 'role-based-login-application'? Should I use Identity System? Or any other session based structure?
Code: Sorry, I have no code to mention here for reference.

Related

Using GUID for MySQL membership provider user

I'm using the MySql membership provider with a .NET MVC 4 application and got it all set up as per this tutorial.
The problem is that the provider sets up the mysql_aspnet_users table with the UserID PK as an INT(11), whereas the MSSQL provider uses a UNIQUEIDENTIFIER.
I need to migrate existing users to this database and I would much prefer to keep a Guid as the primary key within the Users and Membership tables.
Is there any way to do this with the MySql membership provider?
Or do I need to write my own MySql membership provider just to use UUIDs as the primary keys?
I've had a look for any documentation or "non-hacky" ways to do this but haven't had any luck so far.
I dont think you can prevent creating a custom membership user class with a custom membership provider. Something like this tutorial http://msdn.microsoft.com/en-us/library/ms366730%28v=VS.85%29.aspx they use GUID's as wel. You need to change the SQL abit so it works with MySQL
You can store a guid as a CHAR(16) binary if you want to make the most optimal use of storage space.
or varchar(36) if its ok .
http://mysqlbackupnet.codeplex.com/wikipage?title=Using%20MySQL%20With%20GUID%20or%20UUID
At work we ended up creating our own MySql membership provider for .NET and it's called Dolphin https://github.com/film-skills/dolphin-net! It also includes a role provider, and allows you to specify in the config whether you use a GUID or an Integer. It's still in very early days and hasn't been tested thouroughly in production though it's a start! Also on NuGet http://www.nuget.org/packages/Dolphin/0.0.2

How to use your own database structure in Grails?

We have an Android-Application communicating with a MySQL database via SOAP.
Now we were forced to create a webpage with Grails and of course we want to use the same database.
But how can we tell Grails to use our database structure for the domains?
Is there a way to merge these systems?
(The connection to the MySQL-Database is already established, but the two structures do not work together)
e.g. (in the least complicated case) we have a table "locations" with just one column 'name' which is PK. Grails would create a structure for the domain "location" with three columns 'id' 'version' and 'name'.
Grails creators had already thought about you and your case which is a common scenario when someone tries to move to grails with an existing enterprise infrastructure.
You need to have a look at db reverse engineer plugin which creates domain classes based on the existing table structure. You can use the domain classes once created by the plugin.
You can access your MySQL db by providing the datasource as such. In general, company wide datasource would be maintained (or you can create one if required), and use the datasource in Datasource.groovy.
GORM allows you a lot of configurations, you can disable the version control, change your primary key mapping and so on. In your example:
class Locations {
String name
static mapping = {
id column: 'name' //change the id from "id" to name
version false //remove version control, so it will not be added to your table
}
}

How to make portable AND native ID generation in JPA 2 / Hibernate?

I would like to have native and portable id generation on my JPA 2 entities, currently running Hibernate and MySQL
When using #GeneratedValue(strategy=AUTO), hibernate defaults to the "hibernate_sequence" table on MySQL, i would like IDENTITY
If i solve it using #GeneratedValue(strategy=IDENTITY), i loose Oracle/Postgres portability
How can i set Hibernate to use IDENTITY as default for mysql when #GeneratedValue strategy=AUTO?
You can write your own custom generator and maybe invoke a function/stored procedure on your
DB to create the identity you need.
Take a look here , this is a blog with nice example about how to do what I just wrote.
Without code change in Hibernate you cannot affect that. One way around is using different set of mappings (xml instead of annotations) for each database with different id requirements. That is of course quite much to do.
Only truly portable way that works independently from database vendor, is to generate id with TABLE strategy.

GUID of 00000000-0000-0000-0000-000000000000 causing merge index violation

Our developer has a linq-2-sql project that talks to my database. The database is involved in merge replication. It has been in use for some time and was working fine. A recent table was added to the schema and now is causing problems when new records are added.
The user get's an error message stating that the index related to the guid that merge replication automatically creates is violating a unique constraint.
From what I can tell the table isn't any different than others that are involved. I have recreated the entire replication publication/subscription model from scratch and everything continues to work but that one table.
Anyone have any ideas? The guid being created appears as 00000000-0000-0000-0000-000000000000 which would explain why it's a duplicate. Why is a valid guid not being created by linq?
Did you use "new Guid()" somewhere in your code base when what you meant was "Guid.NewGuid()"?
I had faced the similar problem. As Mark has mentioned in the comment, the Guid() needs to be properly used.
Guid asm = new Guid(); // gives 00000000-0000-0000-0000-000000000000
Instead use
Guid asm = Guid.NewGuid();
When using Linq-To-SQL, make sure the IsDbGenerated property is true and the Database actually is setup to create an ID (by using newid() as the default value).
Otherwise, make sure the .net code is actually generating IDs.
What we discovered while researching your suggestions was that this particular table was the only table that included the guid field at all in the DBML class. All the other tables had been added to the DBML prior to publishing the database for merge replication (hence their respective guid fields were not included in the DBML).
So, I manually deleted the guid field from the problem table in the DBML and the problem went away. The problem was in fact caused by LINQ not creating the guid as it should in the generated classes.
In this case it was easiest to simply leave guid creation to the publication triggers and newid() default value as established in SQL. (it's still in the database, just not the dbml)
Nothing in the application uses those guid fields... it's purely for SQL to manage the merge replication scheme we've implemented - so removing from the DBML was the easiest.

LINQ to SQL : Need Different Usernames for Prod and Dev DBML On Table Attribute

This is a similar problem to this question
When I deployed my dev app to the prod server I was getting an error:
System.Data.SqlClient.SqlException: Invalid object name 'dbo.Login'.
The table attribute on the linq mapping looks like [Table(Name="dbo.Login")]
When I loaded the prod db schema into the vs2008 server explorer and refreshed my dbml entirely with that one, the application on prod works, but now the application on dev does not work.
The table attribute on the linq mapping now looks like [Table(Name="prodDbUsername.Login")]
On the dev server I now get
System.Data.SqlClient.SqlException: Invalid object name 'prodDbUsername.Login'.
What is the best way to manage these different user prefixes between dev and prod?
It sounds like you have have identical tables named differently in your different environments. The simplest way to fix your problem is to use identical schema for all environments (the data can be different, but you are asking for all kinds of problems if the schema is not the same).
EDIT: With your further clarification, the tables are being created either with a different owner or a within a different schema. See http://www.sqlteam.com/article/understanding-the-difference-between-owners-and-schemas-in-sql-server for further clarification on the difference.
I would recommend that you try creating your tables with the following syntax:
CREATE TABLE [dbo].[MY_TABLE_NAME]...
If you're using a single schema (i.e. not multiple schemas in a single db), you can just remove the schema prefix on your tables.
E.g. change:
<Table Name="dbo.person" Member="Persons">
To:
<Table Name="person" Member="Persons">