c# - Entity Framework 6.1.1 Code First create unreadable table -
i have problem code:
class program { static void main(string[] args) { database.setinitializer(new dropcreatedatabasealways<mycontext>()); using (var context = new mycontext()) { var newtype = new systype { name = "new name" }; context.systypes.add(newtype); context.savechanges(); } using (var context = new mycontext()) { console.writeline(context.systypes.firstordefault()); } console.readline(); } } public class systype { public int id { get; set; } public string name { get; set; } } public class mycontext : dbcontext { public mycontext() : base("name=mydb") { } public dbset<systype> systypes { get; set; } }
during execution program exception happens:
system.data.entity.infrastructure.dbupdateexception"
system.data.sqlclient.sqlexeption: ad hoc updates system catalogs not allowed.
but database created (to post images not allowed me)
![sql server object explorer view]
if try query table's data, exception thrown
![exception if query table data]
if create tables manually using sql script, table query successful.
Аfter many hours, realized because of name of table.
if change table name - work perfectly.
my question is: why table name systype
throw exception? Аnd there other names of tables cause similar problems?
i glad hear answer question
update: model first approach - same result
because entity have created dbo.systypes
existing system table in database dbo.systypes
.
your entity generated dbo.systypes
because dbo
default schema name when generating table , systype
pluralized systypes
pluralizingtablenameconvention
.
the solution configure manually generated table name, can either using attribute:
[tableattribute("application_systypes")]
or using fluent api:
modelbuilder.entity<systype>().totable("application_systypes")`
Comments
Post a Comment