0

I'm trying to learn fluent and I thought I'd start with a simple example. Turns out I must be missing something because fluent keeps trying to create a table twice resulting in "There is already an object named 'Author' in the database."

I've got the following two data classes:

  public class Author
{
    public virtual int Id {get;set;}

    public virtual string name {get;set;}

    public virtual IList<Book> books { get;protected set; }

    public Author()
    {
        books = new List<Book>();
    }


    public virtual void AddBook(Book book)
    {
        books.Add(book);
        book.authors.Add(this);
    }
}

public class Book
{
    public virtual int Id { get; set; }

    public virtual string name { get; set; }

    public virtual IList<Author> authors {get; protected set; }

    public Book()
    {
        authors = new List<Author>();
    }

    public virtual void AddAuthor(Author author)
    {
        authors.Add(author);
        author.books.Add(this);
    }

}

And i'm providing the following overrides:

    public class BooksOverride : IAutoMappingOverride<Book>
{
    public void Override(AutoMapping<Book> mapping)
    {
        mapping.HasManyToMany(map => map.authors)
            .Cascade.All()
            .Inverse()
            .Table("BooksAuthors");

    }
}

public class AuthorOverride : IAutoMappingOverride<Author>
{
    public void Override(AutoMapping<Author> mapping)
    {

        mapping.HasManyToMany(map => map.books).Cascade.All().Table("BooksAuthors");

    }
}

Here is the insert code:

  using (var session = HibernateHelper.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {

                var author1 = new Author { name = "john"};
                var author2 = new Author { name = "william" };

                var book1 = new Book { name = "A book"};

                author1.AddBook(book1);

                session.Save(author1);
                transaction.Commit();



            }
        }

Now I can't for the life of me work out why this doesn't work!

It seems I have a similar problem to this: Fluent Nhibernate Many to Many Mapping Way

but I have no idea how to fix it. Perhaps this is something to do with the fact I'm using automapping?

If anyone has any ideas I'd be extremely grateful.

Many thanks!

Community
  • 1
  • 1
Chris Birch
  • 2,041
  • 2
  • 19
  • 22

1 Answers1

0

"There is already an object named 'Author' in the database."

Take a look in the database to make sure there has not Author table.