【发布时间】:2019-02-08 11:07:17
【问题描述】:
这简直把我逼疯了。我创建了一个表格,如下图所示:
这是用户实体的流畅配置:
//this is the User entity model class
public class User
{
public long Id { get; set; }
public string EmailAddress { get; set; }
public string HashedPassword { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int IsAdmin { get; set; }
}
public class UserEntityModelBuilder : IEntityModelBuilder
{
public void Build(DbModelBuilder modelBuilder)
{
var config = modelBuilder.Entity<User>();
config.Property(e => e.Id).HasColumnName("ID");
config.Property(e => e.EmailAddress).HasColumnName("EMAIL");
config.Property(e => e.HashedPassword).HasColumnName("PASSWORD");
config.Property(e => e.FirstName).HasColumnName("NOME");
config.Property(e => e.LastName).HasColumnName("COGNOME");
config.Property(e => e.IsAdmin).HasColumnName("ADMIN");
config.ToTable("UTENTIEROGAZIONE").HasKey(e => e.Id);
}
}
现在我正在运行一个非常简单的 LINQ 语句来测试登录,它抛出了这个奇怪的异常消息:
事实上我只使用了 VARCHAR2 字段,我不明白到底发生了什么……有什么线索吗?
编辑(添加 DbContext 初始化):
public BarcodePrinterDbContext(IConnectionStringProvider connectionStringProvider,
IEnumerable<IEntityModelBuilder> entityModelBuilders)
: base(connectionStringProvider.GetConnectionString())
{
_entityModelBuilders = entityModelBuilders ??
throw new ArgumentNullException(nameof(entityModelBuilders));
Database.SetInitializer(
new NullDatabaseInitializer<BarcodePrinterDbContext>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//setting the schema for Oracle DB
SchemaSetup.SetupSchema(modelBuilder);
//registering all entities fluent configurations on the model builder
foreach (var entityModelBuilder in _entityModelBuilders)
entityModelBuilder.Build(modelBuilder);
/*
* I googled out something on wrong mappings on string types,
* so I tried to set all string fields to a maximum of 2000
* characters, unfortunately with no success.
*/
modelBuilder
.Properties()
.Where(p => p.PropertyType == typeof(string) &&
p.GetCustomAttributes(typeof(MaxLengthAttribute), false).Length == 0)
.Configure(p => p.HasMaxLength(2000));
}
【问题讨论】:
-
EmailAddress 不是一个类。你不想要 emailAddress.EMAIL 吗?
-
@jdweng nope:EmailAddress 是一个字符串属性,emailAddress 是一个字符串参数
-
为什么要使用string.Equals?换成 == 会不会出现同样的错误?
-
@DevilSuichiro 是的,已经尝试过同样的异常
标签: c# .net oracle entity-framework linq