Duplicated reference key - Fluent NHibernate Automapping - nhibernate

I got a problem with automapping in fluent and reference key. Example would be that:
public class ConfigurationCategory
{
public virtual Guid Id { get; private set; }
[NotNull]
public virtual String Name { get; set; }
public virtual String Description { get; set; }
public virtual String Icon { get; set; }
public virtual ConfigurationCategory Parent { get; set; }
public virtual IList<ConfigurationCategory> Children { get; private set; }
public ConfigurationCategory()
{
Children = new List<ConfigurationCategory>();
}
}
Results in the following SQL-Output:
CREATE TABLE "ConfigurationCategory"
...
parent_id uuid,
configurationcategory_id uuid,
CONSTRAINT "ConfigurationCategory_pkey" PRIMARY KEY (id),
CONSTRAINT fk6ccc850055890dc8 FOREIGN KEY (configurationcategory_id)
REFERENCES "ConfigurationCategory" (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk6ccc8500ee71b726 FOREIGN KEY (parent_id)
REFERENCES "ConfigurationCategory" (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
Why is ConfigurationCategory duplicated?

I haven't used fluent automapping, but I would guess it is confused by the fact that you have both the Parent and the Children properties; I would guess fluent can't tell that they are both meant to be handled by the same column in the database.
You probably need to create a ClassMap and specify the key column names for both the References() and HasMany() calls.

Related

Asp.net core Add Soft delete by modelBuilder.Entity<DependenciaDisciplina>().Property<bool>("isDeleted"); but isDeleted as composite key

I insert the soft delete flag with
modelBuilder.Entity<DependenciaDisciplina>().Property<bool>("isDeleted");
But i need to add it as a composite key
Can somebody help me?
I insert the soft delete flag with
modelBuilder.Entity<DependenciaDisciplina>().Property<bool>("isDeleted");
But i need to add it as a composite key
To set the Composite Keys using the Fluent API, we have to use the HasKey() method.
After check the HasKey() method definition, we can see that the parameter should be a string array or an expression.
So, you could use the set the composite key like this (change the Car model to your model):
modelBuilder.Entity<Car>().Property<bool>("isDeleted");
modelBuilder.Entity<Car>().HasKey(new string[] { "CarId", "isDeleted" });
The Car model:
public class Car
{
[Key]
public int CarId { get; set; }
public string CarName { get; set; }
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
Then, after migration, the result like this:

EF CORE CODE FIRST PRIMARY KEY COMPOSITE KEY PRIMARY KEY

I would like to know if someone knows how to make a table with a primary key composed of two columns, where the first column is sent by me, and the second is generated from the first
public class Person
{
public int idPerson { get; set; }
public string name { get; set; }
}
public class PersonAdress
{
public int idPerson { get; set; }
public int DireccionId { get; set; }
public string reference { get; set; }
}
I am looking for the incremental of the second column to be if the first column changes
how to make a table with a primary key composed of two columns
You can add the following code by fluent api in dbContext's OnModelCreating method :
modelBuilder.Entity<PersonAdress>().HasKey(sc => new { sc.idPerson , sc.DireccionId });
You can also have a reference for this.

Create a hierarchical foreign key to Identity 3 ApplicationUser:IdentityUser

I am using Identity 3 in ASP.NET 5 RC1/Core
I need to create a foreign key reference in the ApplicationUser that refers to the primary key of the ApplicationUser(IdentityUser) using EF Code First.
I have tried
public class ApplicationUser : IdentityUser
{
[Column("fk_LineManagerUserId")]
public Guid LineManagerUserId { get; set; }
[ForeignKey("LineManagerUserId")]
public virtual ApplicationUser LineManagerUser { get; set; }
}
or
public class ApplicationUser : IdentityUser
{
[Column("fk_LineManagerUserId")]
public string LineManagerUserId { get; set; }
[ForeignKey("LineManagerUserId")]
public virtual ApplicationUser LineManagerUser { get; set; }
}
but I get
Introducing FOREIGN KEY constraint 'FK_ApplicationUser_ApplicationUser_LineManagerUserId' on table 'AspNetUs
ers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Fluent nHibernate - Wrong column name when self reference

The mapping on foreign key are made with the wrong name. Why?
Here's my classes:
The order of the properties seems to be important:
public class Person
{
public virtual Person Mother { get; set; }
public virtual IList<Item> Items { get; set; }
public virtual Person Father { get; set; }
}
public class Item
{
public virtual string Name { get; set; }
}
Here's the mapping with Fluent Nhibernate
AutoMap.AssemblyOf<Person>(new CustomAutomappingConfiguration())
When I look to the database, the foreign key in the table seems to be the name of the first property with the type Person after the property Items. Here's the SQL generated to create the table:
CREATE TABLE "Item" (Id integer primary key autoincrement
, Name TEXT
, Father_id BIGINT
, constraint FKC57C4A2B4586680 foreign key (Father_id) references Patient)
Thank you in advance for your help ;)
The solution I've found is to override the configuraton like this:
AutoMap.AssemblyOf<Person>(new CustomAutomappingConfiguration())
.Override<Person>(m => m.HasMany<Item>(x => x.Items).KeyColumn("Patient_Id"))
Does exist any solution to let the auto mapping work seamlessly? And how Fluent nHibernate works to choose the name of the foreign key's column?

Fluent NHibernate Mapping not on PK Field

I have the following tables and cannot edit their structure...
Person
------
Id PK
Code
Name
Order
-----
Id PK
Person_Code
OrderDetails
Now in my Person class I want to have a list of Orders for that person, but I'm not entirely sure how to go about setting up the mapping in fluent nhibernate to match on the Code column rather than the ID. There is no foreign key constraint and I am unable to change the database to use the keys. Something like this is what I require, but can;t seem to figure out the mapping.
public class Person
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual IList<Order> Orders { get; private set; }
}
public class Order
{
public virtual int Id { get; set; }
public virtual string OrderDetails { get; set; }
public virtual Person Owner { get; set; }
}
You define your column with the KeyColumn method. It should work regardless of existence of a foreign key constraint.
class PersonMap : ClassMap<Person>
{
public PersonMap()
{
HasMany(p => p.Order)
.KeyColumn("Person_Code")
.PropertyRef("Code");
}
}
PropertyRef method is available from rev 614 so you may need to update the fluent nhibernate version.