0

I am using fluent nHibernate for my mappings as follow:

public class ContentTagMap: ClassMap<Employee>
{
    public EmployeeMap()
    {
        Id(t => t.Id);
        Map(t => t.Name);
        HasManyToMany(t => t.Company);
    }
}

public class CompanyMap: ClassMap<Company>
{
    public HelpMap()
    {
        Id(h => h.Id);
        Map(h => h.CompanyName).Length(6000);
        Map(h => h.address).Length(6000);
        HasManyToMany(h => h.Employee);
    }
}

These mappings produce Employee Table ,Company Table and EmployeeToCompany Table

Employee Table

Id    Name
1     John
2     MAX

Company Table

Id    CompanyName   address
1      HTC           ABC
2      HTC2          India

EmployeeToCompany Table

Employee_Id     Company_Id
1               1
2               1

How can I delete the employee with Id 1?

JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
Sreenath Plakkat
  • 1,765
  • 5
  • 20
  • 31

1 Answers1

1

Unless I'm misunderstanding your question you should be asking:

How can i delete the content of the table using NHibernate?

Fluent NHibernate is only a tool to aid in the mapping of your entities. NHibernate is what you use to create, read, update and delete data. In any event:

9.5. Deleting persistent objects

ISession.Delete() will remove an object's state from the database. Of course, your application might still hold a reference to it. So it's best to think of Delete() as making a persistent instance transient.

From the NHibernate Documentation

You probably want to also define a Cascading style on your many to many relationship (HasManyToMany) in your mapping.

If you use Cascade.SaveUpdate in your many to many whenever you delete an entity on one side of the relationship it will delete that entity and any relationships. If you remove the association (ex. if you removed an Employee from a Company) it will only delete the relationship (EmployeeToCompany). This is what I've typically found to work in the case of many to many relationships.

Look at this article for more details on mapping and using a many to many relationship in Fluent NHibernate.

how to keep many to many relationships in sync with nhibernate?

Community
  • 1
  • 1
Cole W
  • 15,123
  • 6
  • 51
  • 85