NHibernate Creating Randomly Id - winforms

I have 2 Classes and 2 Mapping Itens, when I CreatingSchema my NHibernate is creating a Randomly ID Foreign Key on my SQLite database, I dont have 'ID' on my code, I dont know where it is coming from.
Class
public class Locacoes{
public Locacoes()
{
}
public virtual int Codigo { get; set; }
public virtual string Data { get; set; }
public virtual string DataFesta { get; set; }
public virtual string HoraInicio { get; set; }
public virtual string HoraFim { get; set; }
public virtual int TipoEspaco { get; set; }
public virtual string NomeCelebrante { get; set; }
public virtual string Cep { get; set; }
public virtual string Endereco { get; set; }
public virtual string Complemento { get; set; }
public virtual string Bairro { get; set; }
public virtual string Cidade { get; set; }
public virtual string Uf { get; set; }
public virtual string Fone { get; set; }
public virtual string Obs { get; set; }
public virtual double Desconto { get; set; }
public virtual double ValorTotal { get; set; }
public virtual TemaFesta CodTemaFesta { get; set; }
}
}
2.Class
public class TemaFesta
{
public TemaFesta()
{
}
public virtual int Codigo { get; set; }
public virtual string Descricao { get; set; }
public virtual ISet<Locacoes> Locacoes { get; set; }
}
}
Sorry. I cant post my Mapping Codes herem SO is not showing it, so i post pics
1. Mapping
Fist Mapping
2. Mapping
Mapping2
and then, my SQlite creating statement
CREATE TABLE Locacoes (Codigo integer primary key autoincrement, Data TEXT, DataFesta TEXT, HoraInicio TEXT, HoraFim TEXT, TipoEspaco INT, NomeCelebrante TEXT, Cep TEXT, Endereco TEXT, Complemento TEXT, Bairro TEXT, Cidade TEXT, Uf TEXT, Fone TEXT, Obs TEXT, Desconto DOUBLE, ValorTotal DOUBLE, CodTemaFesta INT, id INT, constraint FK1C57939D5747171D foreign key (CodTemaFesta) references TemaFesta, constraint CodTemaFesta foreign key (id) references TemaFesta)
"constraint CodTemaFesta foreign key (id) references TemaFesta)"
I dont have it, so why is nhibernate creating it?

There's nothing random going on here.
The problem is the "set" mapping.
You are declaring a foreign-key="CodTemaFesta" which is then getting generated with your CreateSchema statement. I'm not sure it you want to create this db foreign key, but that's what you declared.
If you just are mapping this in code you need to give it a column name like this
<key column="<column-name>" />

Related

The INSERT statement conflicted with the FOREIGN KEY. Entity includes ForeignKey Id property

I'm doing one to many relationship database with Entity Framework with an Id property.
I have two model classes:
public class PersonModel
{
[Key]
public int PersonId { get; set; }
public string NickName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int TeamRefId { get; set; }
[ForeignKey("TeamRefId")]
public virtual TeamModel TeamModel { get; set; }
}
public class TeamModel
{
public TeamModel()
{
TeamMembers = new List<PersonModel>();
this.Tournaments = new HashSet<TournamentModel>();
}
[Key]
public int TeamId { get; set; }
public string TeamName { get; set; }
public virtual ICollection<PersonModel> TeamMembers { get; set; }
public virtual ICollection<TournamentModel> Tournaments { get; set; }
public virtual MatchUpEntryModel MatchupEntry { get; set; }
public virtual MatchUpModel Matchup { get; set; }
}
When I'm trying to create a new Person entity, I get this error:
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.PersonModel_dbo.TeamModel_TeamRefId". The conflict occurred in database "Tournament2", table "dbo.TeamModel", column 'TeamId'.
Making Foreign Key in Person Model nullable should solve your problem
note that created person will have No Team until you Modify it later after your create Team
public class PersonModel
{
[Key]
public int PersonId { get; set; }
public string NickName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int? TeamRefId { get; set; }
[ForeignKey("TeamRefId")]
public virtual TeamModel TeamModel { get; set; }
}

Create multiple foreign keys referes to the same table in EF code first

In my table it has two foreign keys which refers to the same table. When I do the migration(Entity Framework Code first Approach) it pops up error as,
"One or more validation errors were detected during model generation:
Dog_Sire_Target: : Multiplicity is not valid in Role 'Dog_Sire_Target' in relationship 'Dog_Sire'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.".
But if I add only one foreign key it works properly. Here is my table structure.
public class Dog
{
[Key]
public int Dog_Id { get; set; }
public string Dog_Name { get; set; }
[ForeignKey("Sire")]
public int? Dog_SireId { get; set; }
[ForeignKey("Dam")]
public int? Dog_DamId { get; set; }
[ForeignKey("Dog_SireId")]
public virtual Dog Sire { get; set; }
[ForeignKey("Dog_DamId")]
public virtual Dog Dam { get; set; }
}
Try this model:
public class Dog
{
[Key]
public int DogID { get; set; }
public string DogName { get; set; }
public int? DogSireID { get; set; }
[ForeignKey("DogSireID")]
public virtual Dog DogSire { get; set; }
public int? DogDamID { get; set; }
[ForeignKey("DogDamID")]
public virtual Dog DogDam { get; set; }
[InverseProperty("DogSire")]
public virtual ICollection<Dog> DogsSires { get; set; }
[InverseProperty("DogDam")]
public virtual ICollection<Dog> DogsDams { get; set; }
}

Questions About Database

I write chat in C# with Entity Framework
This is my code
public class User
{
[Key]
public long id { get; set; }
public List<UserMessages> userMessages { get; set; }
}
public class UserMessages
{
[Key]
public long Id { get; set; }
public long ChatMateId { get; set; }
public string text { get; set; }
public DateTime? dateTime { get; set; }
}
Entity Framework code-first forced me to put on the Id field in UserMessages class attribute [Key] , I don't need this field at all because UserMessages is weak entity that will point to id of User
Can I delete long Id from sql table after ef create the table, without any problems?
No, you can't. EF requires that all tables have a [Key] on them, or it will blow up. Even when mapping to SQL Views (as I've learned the hard way, believe me).
In your case, you should actually be using a proper Foreign Key in that table also, like so:
public class UserMessages
{
[Key]
public long Id { get; set; }
[ForeignKey("User")]
public long UserId { get; set; }
public long ChatMateId { get; set; }
public string text { get; set; }
public DateTime? dateTime { get; set; }
public User User { get; set; }
}

Better way to create database for a pricing system

I need to create a price table system so I am going to create these three tables in my database.
PricingTable (ID, Name, ServiceID, Style)
PricingTablePackages (ID, PricingTable_ID, Title, Price, PricePerTime, Info, Flag, Link)
PricingTablePackagesFeatures (ID, PricingTablePackages_ID, Feature, Value, MoreInfo)
Here one PriceTable can hold more then one PricingTablePackages and one PricingTablePackage can hold more then one PricingTablePackagesFeature.
Is any way to design a better model? In a single database Table ?
I am going to create a MVC3 Model for those table so what is the best way to do this kind of DB Table in a MVC3 Model?
I would use public virtual variables for 'lazy-loading' values when you need them using Entity Framework:
(variable types may be off depending on exactly what you want for each variable)
public class PricingTablePackages
{
public int ID { get; set; }
public int PricingTableID { get; set; }
public virtual PricingTable PricingTable { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public decimal PricePerTime { get; set; }
public string Info { get; set; }
public bool Flag { get; set; }
public string Link { get; set; }
}
public class PricingTablePackagesFeatures
{
public int ID { get; set; }
public int PricingTableID { get; set; }
public virtual PricingTable PricingTable { get; set; }
public string Feature { get; set; }
public string Value { get; set; }
public string MoreInfo { get; set; }
}
public class PricingTable
{
public int ID { get; set; }
public string Name { get; set; }
public int ServiceID { get; set; }
public virtual Service Service { get; set; } // if there is a Service class
public string Style { get; set; }
}

EF 4.1 Code First Error - IDENTITY_INSERT is set to OFF

I am very new to EF 4.1. I am getting the error in my Code First development in EF 4.1.
"Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF."
I searched in internet and understood abt the usage of the
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)].
But no use. I changed the option from Identity to None also. But no use.
You are trying to insert a new row into a table with an auto increment PK, you shouldnt be setting the ID property here when you try and add it to the DB.
I had this same error because I had defined a foreign key relationship the wrong way in my classes:
public partial class Student
{
[Key]
public int StudentId { get; set; }
public string StudentName { get; set; }
public int StudentAge { get; set; }
public int GradeNumber { get; set; }
public string LockerNumber { get; set; }
public string TeacherID { get; set; }
public string StudentComments { get; set; }
// Navigation properties
[ForeignKey("TeacherID")]
public virtual Teacher Teacher { get; set; }
public virtual GradeLevel GradeLevel { get; set; }
// the code below is incorrect, the StudentClass should have the StudentId
// as the FK entity:
[ForeignKey("StudentId")]
public virtual StudentClass StudentClass { get; set; }
}
The StudentClass class
public partial class StudentClass
{
[Key]
public int RowId { get; set; }
public int StudentId { get; set; }
public int ClassSubjectId { get; set; }
// Navigation properties
[ForeignKey("ClassSubjectId")]
public virtual ClassSubject ClassSubject { get; set; }
// this is the way the relationship should be defined:
[ForeignKey("StudentId")]
public virtual Student Student { get; set; }
}

Resources