This is a migrated thread and some comments may be shown as answers.

How to invoke data validation before Savechanges

1 Answer 95 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Pavel
Top achievements
Rank 1
Pavel asked on 01 Nov 2012, 11:58 AM
I´m trying to make data validation according to this example. I´m creating object and saving him to db, but object should not be saved to db because of validation on Client object. Validation is not working for some reason, because object is saved to db. What am I doing wrong please?
File structure is attached below.

Program.cs
using FluentDataA;
using FluentDataB;
using Telerik.OpenAccess;


namespace ClientNamespace
{
  class Program
  {
    static void Main(string[] args)
    {
      DataContext dataContext = new DataContext();
      ISchemaHandler schemaHandler = dataContext.GetSchemaHandler();


      if (schemaHandler.DatabaseExists() == false)
      {
        schemaHandler.CreateDatabase();
        schemaHandler.ExecuteDDLScript(schemaHandler.CreateDDLScript());
      }


      dataContext.UpdateSchema();


      Client client = new Client
      {
        FirstName = "First name",
        LastName = "Last name",
        Reject = false
      };


      dataContext.Add(client);


      dataContext.SaveChanges();
    }
  }
}



DataContext.cs
using FluentDataA;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Metadata;


namespace FluentDataB
{
  public partial class DataContext : OpenAccessContext
  {
    static MetadataContainer metadataContainer = new FluentDataAMetadataSource().GetModel();
    
    static BackendConfiguration backendConfiguration = new BackendConfiguration()
    {
      Backend = "mssql"
    };


    private const string DbConnection = "connectionID";


    public DataContext()
      : base(DbConnection, backendConfiguration, metadataContainer)
    {
    }
    
    public void UpdateSchema()
    {
      var handler = this.GetSchemaHandler();
      string script = null;
      try
      {
        script = handler.CreateUpdateDDLScript(null);
      }
      catch
      {
        bool throwException = false;
        try
        {
          handler.CreateDatabase();
          script = handler.CreateDDLScript();
        }
        catch
        {
          throwException = true;
        }
        if (throwException)
          throw;
      }


      if (string.IsNullOrEmpty(script) == false)
      {
        handler.ExecuteDDLScript(script);
      }
    }
  }
}

Client.cs
using System.ComponentModel.DataAnnotations;
 
namespace FluentDataA
{
  public partial class Client : ClientBase
  {
  }
 
  [MetadataType(typeof(ClientBase.ClientBaseMetadata))]
  public partial class ClientBase
  {
    public int ClientId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Reject { get; set; }
 
    public ClientBase()
    {
    }
 
    internal sealed class ClientBaseMetadata
    {
      public ClientBaseMetadata()
      {
      }
 
      [StringLength(2)]
      [Required()]
      public string FirstName { get; set; }
    }
  }
}

FluentDataAMetadataSource.cs
using System.Collections.Generic;
using Telerik.OpenAccess.Metadata;
using Telerik.OpenAccess.Metadata.Fluent;
 
namespace FluentDataA
{
  public class FluentDataAMetadataSource : FluentMetadataSource
  {
    protected override IList<MappingConfiguration> PrepareMapping()
    {
      List<MappingConfiguration> configurations = new List<MappingConfiguration>();
  
      MappingConfiguration<ClientBase> baseClientConfiguration = new MappingConfiguration<ClientBase>();
      baseClientConfiguration.MapType().Inheritance(Telerik.OpenAccess.InheritanceStrategy.Horizontal);
 
      MappingConfiguration<Client> clientConfiguration = new MappingConfiguration<Client>();
      clientConfiguration.MapType(x => new
      {
        ClientId = x.ClientId,
        FirstName = x.FirstName,
        LastName = x.LastName,
        Reject = x.Reject
      }).ToTable("Client");
      clientConfiguration.HasProperty(x => x.ClientId).IsIdentity(KeyGenerator.Autoinc);
      clientConfiguration.HasProperty(x => x.FirstName).IsNotNullable().HasLength(50);
 
      configurations.Add(baseClientConfiguration);
      configurations.Add(clientConfiguration);
 
      return configurations;
    }
  }
}


App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <add name="connectionId" 
         providerName="System.Data.SqlClient" 
         connectionString="server=server; DATABASE=Telerik; USER ID=user; PASSWORD=pass; Application Name=WEB_APP; min pool size=1; max pool size=200; Packet Size=4096;"/>
  </connectionStrings>
</configuration>

1 Answer, 1 is accepted

Sort by
0
Accepted
PetarP
Telerik team
answered on 05 Nov 2012, 01:54 PM
Hello Pavel,

 I am afraid that those attributes are intended to be used with the ria services implementation and not a normal console application setup. If you would like to have such a validation in scenarios not including RIA services I would advice you to have a look into OpenAccess eventing system as you can plug there and do the checks you require.
In particular you should be interested in the PreStore method as there you can put any validation logic your project needs.

Regards,
Petar
the Telerik team
Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
Tags
General Discussions
Asked by
Pavel
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or