Values by default MVC with entity framework and stored procedures

0

I have a stored procedure for saving records that has default values.

Guardar(@ID int, @autor varchar (20)='anonimo',@isbn int,@titulo varchar(25)='TBD',
@descripcion varchar(250)='N/A')




public virtual int Guardar(ObjectParameter ID, string autor,int isbn ,string titulo,string descripcion )
        {
            var autorParameter = autor != null ?
                new ObjectParameter("Autor", autor) :
                new ObjectParameter("Autor", typeof(string));

            var isbnParameter = isbn != null ?
                new ObjectParameter("Isbn", isbn) :
                new ObjectParameter("Isbn", typeof(int));

        var tituloParameter = titulo != null ?
                new ObjectParameter("Titulo", titulo) :
                new ObjectParameter("Titulo", typeof(string));

        var descripcion Parameter = descripcion  != null ?
                new ObjectParameter("Descripcion ", descripcion ) :
                new ObjectParameter("Descripcion ", typeof(string));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("Guardar", ID, autorParameter, isbnParameter, tituloParameter, descripcionParameter);
        }

When compiling it works putting all the data in the textbox but if I do not put anything in some textbox that has value for defualt it marks me error in db.SaveChanges();

Mensaje de error "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

Is there any way in which the application accepts the default values of the store procedure?

I'm using visual 2017 and sql server 2014

    
asked by Ersuka6 13.07.2017 в 16:26
source

1 answer

0

When you do this:

var autorParameter = autor != null ?
                new ObjectParameter("Autor", autor) :
                new ObjectParameter("Autor", typeof(string));

You are creating the parameter but it remains in an uninitialized state, that can cause problems with the EF. Try passing DBNull.Value for null values.

 if (autor != null)
    {
        autorParameter = new ObjectParameter("Autor", autor);
    }
    else
    {
        autorParameter = new ObjectParameter("Autor", typeof(string))
        autorParameter .Value = DBNull.Value;
    }
    
answered by 13.07.2017 в 16:53