0

I have a model which is posted to my backend from a cshtml view. This model is also a representation of a Database-Table.

This code looks for the HTTP POST like the following:

[HttpPost]
public IActionResult CreateAKG(Conversation akg)
{
    if (ModelState.IsValid && ValideD3OrD4Values(akg))
    {
        akg.RDPflichfelderBefuellt = true;
    }
    else
    {
        akg.RDPflichfelderBefuellt = false;
    }
    if (akg.Kommentare == null)
    {
        akg.Kommentare = new List<Kommentar>();
    }
    if (akg.AuftragsklaerungsgespraechId == 0)
    {
        this.MyDatabase.Conversation.Add(akg);
    }
    else
    {
        this.MyDatabase.Conversation.Update(akg);
    }
    this.MyDatabase.SaveChanges();
    return RedirectToAction("Index");
}

The class which represents the model is called Conversation. There are some properties which are annotated by Validation Annotations. The annotation should only be used by the Controller / ModelState.IsValid and not for the Database-Table.

Here is the code sample:

public class Conversation
{
    public int ConversationId { get; set; }

    [Required(ErrorMessage = "This field is required.")]
    public DateTime? DatumAKG { get; set; }

    [MaxLength(256, ErrorMessage = "This field cannot be more than 256 characters")]
    public string KontierungFertigungskosten { get; set; }

    [MaxLength(256, ErrorMessage = "This field cannot be more than 256 characters")]
    public string KontierungQVP { get; set; }

    [MaxLength(256, ErrorMessage = "This field cannot be more than 256 characters")]
    public string KontierungLayoutkosten { get; set; }

    [MaxLength(256, ErrorMessage = "This field cannot be more than 256 characters")]
    public string KontierungBeschaffung { get; set; }
    public bool RDPflichfelderBefuellt { get; set; }
}

The ModelState.IsValid is only used to validate if a boolean is true or false. Other validation is not needed.

My problem now is, that in reason of the data-annotation a string which could normally be NULL in a Database is now in the database-design configured as NOT NULL.

If i try to store a new conversation in the database, a error is throw that some string values could not be null.

What i want to do is:

  1. Validation on Controller

  2. No Validation Annotation which changes the Database-Design

1 Answer 1

1

Since properties and requirements are different you should have 2 different objects

// this is you database object
public class Conversation {
        [MaxLength(256, ErrorMessage = "This field cannot be more than 256 characters")]
        public string KontierungQVP { get; set; }

}

// this your Data Transfer Object
public class ConversationDTO {
        [MaxLength(256, ErrorMessage = "This field cannot be more than 256 characters")]
        [Required]
        public string KontierungQVP { get; set; }
}

Your EF models should always represent the database. If you want to add extra validations or conditions you should do it with another object and simply transfer between those objects. Having 2 different types of objects gives you more modularity.

EDIT:

There is a way to do what you ask but it's not a recommended approach and it might cause issues. You want to use 2 contexts and configure the required properties using FluentAPI and not data annotations.

// Call this method in your context.
protected override void OnModelCreating(your_builder){
    modelBuilder.Entity<Conversation>()
        .Property(p => p.KontierungQVP)
        .IsRequired();
}

Generally you want to have 2 different contexts. One where you will initialize your database. And another one where you will have your required attributes defined by the FluentAPI (not data annotations).

So to recap one DbContext for your DB creation and one other for your operations. Of course this will lead to disparencies and it is easy to forget a validation on the database and so on.

Sign up to request clarification or add additional context in comments.

2 Comments

is there no possibility to activate or deactivate the validation? i only need the validation in http post - in the database there shouldn't be any validation
I changed the question a little bit - maybe there is a possibility. Thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.