Problem saving

Asked

Viewed 97 times

1

Only a form that is returning me this mistake, the question that I do not know what can be or how to fix, some ask to cascade the relationship that is already.

Nhibernate.Transientobjectexception: Object References an Unsaved Transient instance - save the Transient instance before Flushing or set Cascade action for the Property to Something that would make it autosave. Type: Blogweb.Models.Vehicle, Entity: Blogweb.Models.Vehicle

Error return line:

public void Adiciona(Abastecimento abastecimento)
    {
        ITransaction tx = session.BeginTransaction();
        session.Save(abastecimento);
        tx.Commit();
    }

Vehicle Mapping:

public class VeiculoMapping : ClassMap<Veiculo>
{
    public VeiculoMapping()
    {
    Id(p => p.Id).GeneratedBy.Identity();
    Map(p => p.NCarro);
    Map(p => p.Modelo);
    Map(p => p.Ano);

    }
}

Veiculo Model:

public class Veiculo
{
    public virtual int Id { get; set; }
    [Required]
    public virtual int NCarro { get; set; }
    [Required]
    public virtual string Modelo { get; set; }
    public virtual int Ano { get; set; }
}

Supply Mapping:

public class AbastecimentoMapping : ClassMap <Abastecimento>
    {
        public AbastecimentoMapping()
        {
            Id(a => a.Id).GeneratedBy.Identity();
            Map(a => a.DtAbastecido);
            Map(a => a.Litro);
            Map(a => a.VlrUnit);
            Map(a => a.Km);
            Map(a => a.TotalGasto);
            Map(a => a.Km_Andado);
            References(a => a.NomeProduto, "NomeProdutoId");
            References(a => a.Autor, "AutorId");
            References(a => a.NumCarro, "NumCarroId");
        }
    }

Abastecimento Model:

public class Abastecimento
{
    public virtual int Id { get; set;}
    [Required]
    public virtual int Litro { get; set; }
    public virtual DateTime? DtAbastecido { get; set; }
    public virtual decimal VlrUnit { get; set; }
    public virtual int Km { get; set; }
    public virtual decimal TotalGasto { get; set; }
    public virtual int Km_Andado { get; set; }
    public virtual Usuario Autor { get; set; }
    public virtual Compra NomeProduto { get; set; }
    public virtual Veiculo NumCarro { get; set; }
}
  • If you formatted the PC, you brought the database beyond the project?

  • Yes, I brought everything including the database, I have three backups of the bank that is on the servers, on the Storage and on the computer itself

  • @Emanuelf The rest of the form, it saves quietly, only that of the problem

  • Refuelling is referenced the Vehicle?

  • @Emanuelf Supply picks up Id of the Vehicle that is on the table vehicle

  • Just to discharge consciousness. This vehicle is already saved in the system?

  • @Emanuelf I discovered the error, because of a spelling error, when I put to call the class of the Vehicle, the variable is Id and I had put to call by id and not Id

  • I was looking for something that didn’t exist, and returning the error. Solved, perfect!

  • @Emanuelf Don’t know the hate that I hate, I stayed 3 days with this problem and I find something stupid, but thanks for the attention and patience

Show 4 more comments

1 answer

1


You are saving an entity, which has a relationship with an entity that has changed but has not been saved before.

If this is the case, you have two options:

  • Save the other entity before saving this one;

  • Map the relationship as cascade, so that in such a situation the other related entity is also saved.

Speaking of Easter

For every basic operation of the Nhibernate session - including Persist (), Merge (), SaveOrUpdate (), Delete (), Lock (), Refresh (), Evict (), Replicate () - there is a corresponding cascade style. Respectively, cascading styles are called persist, merge, save-update, delete, block, update, expel, replicate. The cascading style for Save () and Update () is save-update; to SaveAndUpdateCopy () is merged; and PersistOnFlush () is persistent. And removing is an alias to delete.

If you want an operation to be cascaded over an association, you should indicate this in the mapping document. For example:

<one-to-one name="person" cascade="persist"/>

Cascading styles of mine combined:

<one-to-one name="person" cascade="persist,delete,lock"/>

You can use cascade = "all" to specify that all operations must be cascaded over the association. The cascade padrão = "none" specifies that no operation should be cascaded.

I recommend reading more in this post in English HERE

  • Emanuel thanks for the explanation, but where I would put Scade in a nhibernate Mapping public class VeiculoMapping : ClassMap<Veiculo>&#xA; {&#xA; public VeiculoMapping()&#xA; {&#xA; Id(p => p.Id).GeneratedBy.Identity();&#xA; Map(p => p.NCarro);&#xA; Map(p => p.Modelo);&#xA; Map(p => p.Ano);&#xA; Map(p => p.Km);&#xA; &#xA; }&#xA; }

  • @Guilloche Hermepadovam you have a Vaiculo.hbm.xml ?

  • I don’t have it, but I have Routeconfig.Cs, I don’t know if it’s something I have to change, the problem that other forms, that make the CRUD perfectly, only the one that isn’t right, but it’s the most important class

  • As said in the reply. This trying to save the Supply Entity, but made some change in the entity Vehicle that was not saved.

  • Strange, that’s what I’m trying to find, because if it was in the bank it would add a column by Mapping, it’s all right, both in the model, Mapping and viewmodel vehicle that’s the problem, because it returns me the error in the vehicle model

  • Edit the response and add the mapping of the two entities.

  • I added in the question both the supply Mapping where I try to save and returns the error and the Mapping and model from where it speaks the error that is the Model Vehicle

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.