Fluent NHibernate: Foreign Key violation or Null values -


Hey guys, I have some real problems with mapping with the notion. I know that there are many posts on this site and Many others focus on specific types of mapping but as of now, I have not found any solutions to solve my solution.

What do I have here:

  Namespace MyProject.Models.Entites {Public Square Project {Public Virtual GUID ID {get; Set;} // A load of other properties in public virtual projectsContagori {get; Set;}}}  

and then map:

  Namespace MyProject .DataAccess.ClassMappings {public class projectmap: classpass & lt; Project & gt; {Public project map () {id (x => x.Id); Map (x = & gt; x.Title); Map (x => x.Description); Map (x => x.LastUpdated); Map (x = & gt; x.ImageData) .CustomSqlType ("image"); HasOne (x => x.Catagory); }}}  

As you can see, I have a project in which there is a catalyst property, I am not so hot on the relational database, but as I can guess, this is a lot There is a relation where many projects can be a catagory. No, projects can not come in more than one category.

So now we have:

  Namespace MyProject.Models.Entities {Public class ProjectCatagory {Public Virtual GUID ID}; Set; } Public Virtual String Name {get; Set; }}}  

and its map:

  public project tagorium () {id (x => x.Id); Map (x = & x; x.Name); }  

The problem is, okay, it does not work! I will do something similar to the following in a unit test:

  project myproject = new project ("project description"); // Other properties set myProject.Catagory = New ProjectCatagory (Guid.New (), "Test Content"); Repository.Save (MyProject);  

Currently, I have tried a number of mapping and database configurations when trying to get it to work. Currently, the project database table has a column, "Category_ID "(Which I did not put there, I think NH has added it as a result of mapping) and I did not want to allow it to nose. However, when this is set up, I get an exception by telling this That I can not insert zero values ​​in the table (even during debugging, I have examined all the properties on project objects and they are not faucets).

Alternatively, I can allow the table to accept the tap in that column and it will only save the project object and save it completely ignoring the property in the category, so when again Is obtained, check to see if the right category has been associated with the project.

If I miss the right, then at one time I would have used projectmaps:

  context (x => x.catagory). .. Column ("Catagory_id") Cascade.All () Not.Nullable ();  

It has changed the exception of "zero values ​​can not be included" for any foreign key violation.

I suspect that the root of this problem comes from the absence of my understanding as a relational database set up, I have other institutions in this project who do not have external dependencies that are absolutely fine with NHibernate Works, chooses any coding issues which may arise due to I create the repository.

Any help greatly appreciated. Thank you.

The main issue here is a general misconception about the "one-by-one" relationship in the relational database And the HasOne is the relational term in the context of mapping mapping in the fluent. (Fluent tries to beautify them slightly, which makes worse than the IMO. HasOne actually means: one-by-one.)

Take a look at this Insert:

HasOne is usually reserved for a special case, generally, you use a contextual reference in most situations (see: I think you have a lot of Are).

Code> several-to-one in an XML mapping file). You get a foreign key in the database which is ProjectCatagory .


refers to the actual one-to-one relationship in a relational database, ideally a primary key synchronization. When two objects share the same primary key, you do not waste the space for extra foreign keys and it is sure to be one-to-one.

To synchronize the primary key, you key one of the others, though it works, you do not need it here.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -