How to Create index on component property - nhibernate

I have the following classes:
public class Event
{
public virtual Guid Id { get; set; }
public virtual string UserName { get; set; }
public virtual EventId EventId { get; set; }
}
public class EventId
{
public virtual Guid EventGuid {get; private set;}
}
I am using a fluent NHibernate automapping in order to map my classes and I override specific properties when needed.
Class: Event is in s_typesToMap and class: EventId is in s_components.
As a result I get the following table generated:
create table "Event" (
Id UNIQUEIDENTIFIER not null,
UserName NVARCHAR(255) null,
EventIdentifierEventGuid UNIQUEIDENTIFIER null,
primary key (Id)
)
I want to create an index on EventIdentifierEventGuid which is a property in Event component.
I tried to do it as follows:
.Override<Event>(obj => obj.Map(x => x.EventId.EventGuid).Index("EventId_index"))
When I generate the ddl I get the following:
create index EventId_index on "Event" (EventGuid)
The expected result should be an index on EventIdentifierEventGuid instead of EventGuid
How can I do it?

Related

Class Diagram for Social Media Platform

In a professional social network, how would I represent connections between users? (like Linkedin) Should I create a connection class for which there would be an instance for every connection between 2 users or is that redundant? Should the user class instead have a self-association (reflexive association)?
Your User class would have a collection of Followings:
public class User
{
// ... the other code is omitted for the brevity
public IEnumerable<User> Followings { get; set; }
}
So if your database has the Following table:
CREATE TABLE Followings(
User_Id INT NOT NULL,
Following_Id INT NOT NULL,
DateCreated DATE NOT NULL,
);
Do not forget to create constraints and foreign keys in your table. Then it is possible to have Following class:
public class Followings {
public UserId int { get; set; }
public FollowingId int { get; set; }
public DateCreated DateTime { get; set; }
}
and then you can write easily the following query:
select * from Following where UserId = x.id -- or vice versa

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.

Entity Framework Code First - using UserId key of Memberships/Users table as Foreignkey in a custom table

I am trying to create a SQL table to include additional user information.
I want it to be created by Migrations in VS2012 (package console => update-database).
I see that there are two tables using UserId column as key: Memberships and Users tables.
I am trying to define the following class and map it to SQL table via Migrator:
[Key]
[Column(Order = 0)]
[ForeignKey("User")]
public Guid **UserId** { get; set; }
[Key]
[Column(Order = 1)]
[ForeignKey("Category")]
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
**public virtual User User { get; set; }**
Although it is obvious in the Users SQL table that UserId column is the key, I get this error message:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'User' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Users' is based on type 'User' that has no keys defined.
What I am missing here? Might be that Microsoft.VisualBasic.ApplicationServices.User / System.Web.Security.MembershipUser classes weren't necessarily mapped to the tables this way and vise versa, and therefore the UserId property is not declared is Key dataannotation?
I am open for other solutions for this problem.
Big Thanks!
I am currently using asp.net mvc4 with Azure db.
When I want to use UserId in other tables from UserProfile. Notice in AccountModels there is class UserProfile:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
Now, let's say your category is created by certain user, you can link category entry to user in following way.
[Table("Category")]
public class Category
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CategoryId { get; set; }
public UserProfile CreatorUserId { get; set; }
}
Otherwise, you can always use UserProfile as model.

Make AskedBy_PersonId the foreign column name(instead of the default AskedBy_id) on Fluent NHibernate

I have these tables:
create table Person
(
PersonId int identity(1,1) primary key,
PersonName nvarchar(100) not null
);
create table Question
(
QuestionId int identity(1,1) primary key,
QuestionText nvarchar(100) not null,
AskedBy_PersonId int not null references Person(PersonId),
QuestionModifiedBy_PersonId int not null references Person(PersonId)
);
And I have these models:
public class Question
{
public virtual int QuestionId { get; set; }
public virtual string QuestionText { get; set; }
public virtual Person AskedBy { get; set; }
public virtual Person QuestionModifiedBy { get; set; }
}
public class Person
{
public virtual int PersonId { get; set; }
public virtual string PersonName { get; set; }
}
I'm using automapping with FluentNHibernate, the referencing properties defaults to these database column names:
AskedBy_id
QuestionModifiedBy_id
How can one make FluentNHibernate make the referencing properties be mapped to this style of foreign column name?
AskedBy_PersonId
QuestionModifiedBy_PersonId
As of now, I'm doing it in manual overriding:
.Override<Question>(x =>
{
x.References(y => y.AskedBy).Column("AskedBy_PersonId");
x.References(y => y.QuestionModifiedBy).Column("QuestionModifiedBy_PersonId");
})
I wanted to remove that overriding and want Fluent NHibernate to automatically make the foreign column name follow the naming pattern above
How can I achieved that with Fluent NHibernate?
Should be easy with IReferenceConvention implementation:
public class ReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.Column(
instance.Name + "_" + instance.Property.PropertyType.Name + "Id");
}
}
NHibernate should be configured to read the conventions (with something like this):
Fluently.Configure()
//... other configuration
.Mappings(m => m.AutoMappings.Add(
AutoMap.AssemblyOf<Person>()
.Conventions.AddFromAssemblyOf<ReferenceConvention>());

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.