Invalid object name 'dbo.TableName' when retrieving data from generated table

前端 未结 5 1279
感情败类
感情败类 2021-01-19 07:23

I\'m using entity framework code first to create my tables. Please note - create the tables, not the DB, since I\'m working on a hosted environment and I don\'t have a user

相关标签:
5条回答
  • 2021-01-19 07:58

    To answer your first question: use the schema created for you by your hosting provider.

    To answer your second question: No there is currently no direct way to change the default schema globally because you cannot modify existing conventions or create new conventions. You can try to hack it.

    For example you can override OnModelCreating and use reflection to get all DbSet<> properties declared in your context. Than you can just use simple loop on these properties and create ToTable mapping call with name of the property as table name and your custom schema. It will require some playing with reflection to make this work.

    Alternatively you can try to do some reusable approach by implementing custom conventions. You can find many different articles about using your own conventions with EF. Some examples:

    • Custom Conventions in Entity Framework Code First v 4.1
    • Conventions in Entity Framework 4.1 Final

    My high level untested idea is following same principle and create assembly level attribute which will be processed by the convention mechanism and applied on all your entities.

    0 讨论(0)
  • 2021-01-19 07:59

    https://stackoverflow.com/a/12808316/3069271

    I had same issue, it was pluralize problem between mapping and db.

    0 讨论(0)
  • 2021-01-19 08:00

    Try to set default schema name to 'dbo' in SQL SERVER.

    http://msdn.microsoft.com/en-us/library/ms173423.aspx

    0 讨论(0)
  • 2021-01-19 08:20

    Ok, for me issue was that I had a table called dbo.UserState and in C# EF was trying to access dbo.UserStates because of pluralization.

    The solution was to put Table attribute above class and specify the exact table name:

    [Table("UserState")]
    public class UserState
    {
        [Key]
        public int UserId { get; set; }
    }
    
    0 讨论(0)
  • 2021-01-19 08:22

    On of the reason for this error is the table named "EventHosts" may not Exist or that table is renamed to some other name please check with that..

    0 讨论(0)
提交回复
热议问题