This question is locked. New answers and comments are not allowed.
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
DataContext.cs
Client.cs
FluentDataAMetadataSource.cs
App.config
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();
}
}
}
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);
}
}
}
}
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>
<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>