Fluent Mapping fails if an enum property is present

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
jszalay
Posts: 5
Joined: Thu 10 Sep 2015 18:53

Fluent Mapping fails if an enum property is present

Post by jszalay » Fri 11 Sep 2015 14:49

The following code throws an exception while in the DataContext constructor.

using System;
using Devart.Data.Linq;
using Devart.Data.Linq.Mapping;
using Devart.Data.Linq.Mapping.Fluent;
using Devart.Data.MySql.Linq.Provider;

namespace TestFluentMapping
{
[Provider(typeof(MySqlDataProvider))]
class Program : DataContext
{
public Table<Entity1> Entity1Table;
public Table<Entity2> Entity2Table;

static void Main(string[] args)
{
using (var session = new Program(GetConnectionString(), GetMappingSource(new MySqlDataProvider())))
{
session.CreateDatabase(false, true);

var entity1 = new Entity1 { Id = "id1", Prop1 = 1, Prop2 = 2 };
var entity2 = new Entity2 { Id = "id2", Prop1 = 3, Prop2 = 4 };

session.Entity1Table.InsertOnSubmit(entity1);
session.Entity2Table.InsertOnSubmit(entity2);

session.SubmitChanges();
}
}

protected static string GetConnectionString()
{
var u = new Uri("http://localhost:3306");
string cs = string.Format("User ID={0}; Password={1}; Host={2}; Port={3}; Database={4};", "discotest", "discotest", u.Host, u.Port, "test_fluent");
return cs;
}

public Program(string connectionString, MappingSource mapping)
: base(connectionString, mapping)
{
Log = Console.Out;
}

private static MappingSource GetMappingSource(DataProvider provider)
{
var builder = new FluentMappingBuilder(provider);

Entity2.Register(builder);

return builder.CreateMappingSource();
}
}

public enum MyEnum
{
EnumValue1 = 0,
EnumValue2 = 1
}

[Table]
public class Entity1
{
// Note this is not mapped
public MyEnum PropEnum { get; set; }

[Column(Name = "Id", IsPrimaryKey = true, DbType = "VARCHAR(255) NOT NULL")]
public string Id { get; set; }

[Column]
public long Prop1 { get; set; }

[Column]
public long Prop2 { get; set; }
}

public class Entity2
{
public string Id { get; set; }
public long Prop1 { get; set; }
public long Prop2 { get; set; }

public static void Register(FluentMappingBuilder builder)
{
builder.Entity<Entity2>()
.FullTableName("Entity2")
.PrimaryKey(p => p.Id);
builder.Entity<Entity2>()
.Property(p => p.Id)
.ServerDataType("VARCHAR(255) NOT NULL")
.NotNullable();
builder.Entity<Entity2>()
.Property(p => p.Prop1);
builder.Entity<Entity2>()
.Property(p => p.Prop2);
}
}
}


The exception is ArgumentException - {"convertType"}, the traceback is

at Devart.Data.MySql.Linq.MySqlDataTypeGenerator.AddMainDefinition(String dataTypeName, ServerDataTypeInfo typeInfo, Boolean isIncludeDimensions, StringBuilder sb)
at Devart.Data.Linq.BaseDataTypeGenerator.GetServerTypeName(String dataTypeName, ServerDataTypeInfo typeInfo, ServerDataTypeGenerationBehaviour behaviour)
at Devart.Data.Linq.Mapping.Fluent.ServerDataTypeConvention.Apply(IPropertyConfiguration configuration, IModelConfiguration modelConfiguration)
at Devart.Data.Linq.Mapping.Fluent.FluentMappingSource.a(i A_0, Type A_1)
at Devart.Data.Linq.Mapping.Fluent.FluentMappingSource.a(MetaModel A_0, Type A_1)
at Devart.Data.Linq.Mapping.MappingSource.a(Type A_0)
at Devart.Data.Linq.Mapping.MappingSource.GetModel(Type dataContextType)
at Devart.Data.Linq.DataContext.a(Object A_0, MappingSource A_1, Type A_2)
at Devart.Data.Linq.DataContext..ctor(String connectionString, MappingSource mapping)
at TestFluentMapping.Program..ctor(String connectionString, MappingSource mapping) in c:\Users\Administrator\linqconnect\TestFluentMapping\ConsoleApplication1\Program.cs:line 38
at TestFluentMapping.Program.Main(String[] args) in c:\Users\Administrator\linqconnect\TestFluentMapping\ConsoleApplication1\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Notice that Entity1 has an enum property (PopEnum). It is not even mapped into the model, but it is the cause for the exception. The code executes with success if this enum property is removed or commented out.

Is there a way to tell fluent to ignore this enum property completely? Thanks for the help!

jszalay
Posts: 5
Joined: Thu 10 Sep 2015 18:53

Re: Fluent Mapping fails if an enum property is present

Post by jszalay » Fri 11 Sep 2015 16:38

Update: The issue can be solved by adding the following line to the code

builder.Entity<Entity1>().Ignore(p => p.PropEnum);

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Fluent Mapping fails if an enum property is present

Post by MariiaI » Tue 15 Sep 2015 05:50

Glad to see that you have found the solution. If you have any further questions, feel free to contact us.

Post Reply