I’m trying to set an unique constraint on the column LoginName of table User. So the following is my code:
public FluentConfiguration GetNHConfig()
{
var cfg = new FreeflyingConfiguration();
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(_connStr))
.Mappings(m => m.AutoMappings
.Add(AutoMap.AssemblyOf<LogOfArticle>(cfg))
.Add(AutoMap.AssemblyOf<LogOfUser>(cfg))
// here is the problem, I think
.Add(AutoMap.AssemblyOf<User>(cfg).UseOverridesFromAssemblyOf<UserMappingOverride>())
.Add(AutoMap.AssemblyOf<Role>(cfg))
.Add(AutoMap.AssemblyOf<Profile>(cfg))
.Add(AutoMap.AssemblyOf<Blog>(cfg))
.Add(AutoMap.AssemblyOf<Comment>(cfg)));
}
public class UserMappingOverride : IAutoMappingOverride<User>
{
public void Override(AutoMapping<User> mapping)
{
// breakpoint is set here and can be hit every time
mapping.Map(x => x.LoginName).Not.Nullable();
mapping.Map(x => x.Email).Unique();
mapping.Map(x => x.Profile.BlogUrl).Unique();
}
}
The tables can be generated, and the breakpiont on line “” can be hit every time. But there is no change in database, which means, generate the Unique constraint, whatever I use UpdateSchema() or BuildSchema().
BTW, no error when executing.
So it’s harder to find the reason. But any suggestion is welcome!
Are
User,Role,Profileetc really in different assemblies? If they aren’t you only need one.Add(AutoMap.AssemblyOf<T>(). This could be your problem because it might be overwriting your overrides on subsequent calls. I’m not all that familiar with overrides but it’s worth a shot.