实体框架:从数据库更新模型时,Model.edmx修改丢失

时间:2015-02-17 17:29:27

标签: vb.net entity-framework edmx

我正在使用Entity Framework和数据库First我从SQL服务器上的现有数据库创建了model.edmx。 由于某些原因,我在模型中做了一些更改。 例如,我实现了InotiFyPropertyChanged,在某些类中,我更改了向导生成的原始代码。 但是当我在数据库上更改某些内容时,我从数据库更新模型,实现上述功能所做的所有更改都将丢失,我必须从开始编写所有更改。 从数据库更新模型时有没有办法保留这些更改? 或者另一种方法如何解决这个问题? 谢谢!

已更新:

这是向导生成的原始类:

Imports System
Imports System.Collections.Generic

Partial Public Class Myobj1
    Public Property id As Integer
    Public Property prc As Decimal
    Public Property categ As Integer
End Class

这是包含更改的类:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel

Partial Public Class Myobj1
    Implements INotifyPropertyChanged
    Public Property id As Integer

Private privatprc As Decimal
    Public Property prc() As Decimal
    Get
        Return privatprc
    End Get
    Set(ByVal value As Decimal)
        privatprc = value
        OnPropertyChanged("prc")
    End Set
End Property

Private privatcateg As Integer
    Public Property categ() As Integer
       Get
           Return privatcateg
        End Get
        Set(ByVal value As Integer)
            privatcateg = value
            OnPropertyChanged("categ")
        End Set
    End Property

 Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private Sub OnPropertyChanged(ByVal propertyc As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyc))
    End Sub
End Class

我该怎么办?

更新: 我使用以下内容创建了一个单独的类文件:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports MyProg.MyObj1

   <MetadataType(GetType(MyObj1MetaData))> Partial Public Class Myobj1
Friend NotInheritable Class Myobj1MetaData
  Implements INotifyPropertyChanged
Private privatprc As Decimal
    Public Property prc() As Decimal
    Get
        Return privatprc
    End Get
    Set(ByVal value As Decimal)
        privatprc = value
        OnPropertyChanged("prc")
    End Set
End Property

Private privatcateg As Integer
    Public Property categ() As Integer
       Get
           Return privatcateg
        End Get
        Set(ByVal value As Integer)
            privatcateg = value
            OnPropertyChanged("categ")
        End Set
    End Property

 Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private Sub OnPropertyChanged(ByVal propertyc As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyc))
    End Sub
End Class
 End Class

一切都运行没有任何错误,但问题是我想实现的功能(OnPropertyChanged)没有实现,似乎这个新类没有任何效果。 我能做什么 ? 谢谢!

1 个答案:

答案 0 :(得分:-2)

查看此链接http://forums.asp.net/t/1885808.aspx?Update+edmx+file+without+effecting+currently+generated+model 你可能想看看ryanbesko的回答。 partial类获取一个将其指向元数据类的属性。对不起凌乱的代码。我没有访问VS.这只是为了给你一个基本的想法:

<MetadataType(GetType(MyObj1Metadata))> 
    Public Partial Class MyObj1
    //your class code generated from ef goes here

    End Class
Public Class MyObj1Metadata

<Required(ErrorMessage := "Property is required.")> 
Private privatprc As Decimal
    Public Property prc() As Decimal
    Get
        Return privatprc
    End Get
    Set(ByVal value As Decimal)
        privatprc = value
        OnPropertyChanged("prc")
    End Set
End Property
// don't add any new code here, just match what is in your model classes and add data annotations to them.

End Class