当我向现有模型中添加新表时,实体类型dbip_lookup不属于当前上下文的模型
本文关键字:模型 lookup dbip 不属于 上下文 类型 新表时 添加 实体 | 更新日期: 2023-09-27 18:20:48
我真的需要一些帮助。我从数据库中向.NET模型添加了一个新表,并手动创建了实体类和访问它的引用,但在运行时会发生此错误。。。你能帮我吗?
这个代码就是试图运行它的代码:
Protected Sub ObtieneIdioma()
Try
Dim strHostName As String = System.Net.Dns.GetHostName()
Dim ip As String
ip = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
Dim ip_array() As String = ip.Split(".")
Dim ip_bigint As Int64 = Convert.ToInt64(ip_array(3))
ip_bigint = ip_bigint + Convert.ToInt64(ip_array(2)) * 256
ip_bigint = ip_bigint + Convert.ToInt64(ip_array(1)) * 65536
ip_bigint = ip_bigint + Convert.ToInt64(ip_array(0)) * 16777216
Dim Context = New segProdBDEntitiesPT()
Dim ds = Context.dbip_lookup.Where(Function(x) x.Ip_Start <= ip_bigint AndAlso x.Ip_End >= ip_bigint).Single
--- Bunch of code to determine language ---
Catch ex As Exception
'Error en el servicio
Response.Cookies("Idioma").Value = "ENG"
Response.Redirect("~/Welcome.aspx", False)
End Try
End Sub
这是我的上下文文件:
Partial Public Class segProdBDEntitiesPT
Inherits DbContext
Public Sub New()
MyBase.New("name=segProdBDEntitiesPT")
End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
Throw New UnintentionalCodeFirstException()
End Sub
--- Some other entities ---
Public Property dbip_lookup() As DbSet(Of dbip_lookup)
--- Some other Functions ---
End Class
这是我的实体文件:
Imports System
Imports System.Collections.Generic
--- Other partial classes for other entities ---
Partial Public Class dbip_lookup
Public Property Addr_Type As String
Public Property Ip_Start As Int64
Public Property Ip_End As Int64
Public Property Country As String
End Class
当我将表dbip_lookup添加到我的模型中时,我手动完成了所有这些,我不知道我缺少了什么
最后,这里是抛出的错误:
System.InvalidOperationException detected.
HResult=-2146233079
Message=The entity type dbip_lookup is not part of the model for the current context.
Source=EntityFramework
StackTrace:
en System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
en System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
en System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
en System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
en System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
en Site.ObtieneIdioma() en C:'Users'Carlos Flores'Documents'My Dropbox'CSI'Departamento de Calidad'Interno'CSIGroup'Site.Master.vb:línea 684
InnerException:
请在此处查看SO答案:实体类型<类型>不是当前上下文模型的一部分
尝试在OnModelCreating:中添加类似的内容
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
modelBuilder.Entity(Of dbip_lookup)().ToTable("dbip_lookup")
End Sub