C# (接触) { if(CurrentContacts.Contains(触点)) { 电流触点。移除(触点); } _contactRepository.Delete(contact); StatusText=string.Format(“联系人{0}”已被删除。”,Contact.LookupName); } 公共无效关闭选项卡(演示者基础演示者) { View.RemoveTab(演示者); } 私人void OpenContact(联系人联系人) { if(contact==null)返回; View.AddTab(新的EditContactPresenter(这个,新的EditContactView(),contact)); } public void DisplayAllContacts() { 抛出新的NotImplementedException(); } } } 名称空间CMnew.Presenters { 公共类EditContactPresenter:PresenterBase { 私有只读ApplicationPresenter\u ApplicationPresenter; 私人联络(私人联络);; 公共EditContactPresenter(ApplicationPresenter应用程序Presenter,EditContactView视图,联系人):基本(视图,“Contact.LookupName”) { _applicationPresenter=applicationPresenter; _接触=接触; } 公众接触 { 获取{return\u contact;} 设置{u contact=value;} } public void SelectImage() { 字符串imagePath=View.AskUserForImagePath(); 如果(!string.IsNullOrEmpty(imagePath)) { Contact.ImagePath=ImagePath; } } 公共作废保存() { _applicationPresenter.SaveContact(联系人); } 公共作废删除() { _applicationPresenter.CloseTab(此); _applicationPresenter.DeleteContact(联系人); } 公众假期结束() { _applicationPresenter.CloseTab(此); } 公共覆盖布尔等于(对象对象对象) { EditContactPresenter presenter=对象作为EditContactPresenter; 返回presenter!=null&&presenter.Contact.Equals(Contact); } } } 名称空间CMnew.Views { /// ///EditContactView.xaml的交互逻辑 /// 公共部分类EditContactView:UserControl { 公共EditContactView() { 初始化组件(); } 公共编辑联系人演示者演示者 { 获取{作为EditContactPresenter返回DataContext;} } 私有无效保存\单击(对象发送者,路由目标) { Presenter.Save(); } 私有无效删除\单击(对象发送者,路由目标) { Delete(); } 私有无效关闭\单击(对象发送者,路由目标) { Presenter.Close(); } 私有void SelectImage\u单击(对象发送方,路由目标) { Presenter.SelectImage(); } 公共字符串AskUserForImagePath() { OpenFileDialog dlg=新建OpenFileDialog(); dlg.ShowDialog(); 返回dlg.FileName; } } }

C# (接触) { if(CurrentContacts.Contains(触点)) { 电流触点。移除(触点); } _contactRepository.Delete(contact); StatusText=string.Format(“联系人{0}”已被删除。”,Contact.LookupName); } 公共无效关闭选项卡(演示者基础演示者) { View.RemoveTab(演示者); } 私人void OpenContact(联系人联系人) { if(contact==null)返回; View.AddTab(新的EditContactPresenter(这个,新的EditContactView(),contact)); } public void DisplayAllContacts() { 抛出新的NotImplementedException(); } } } 名称空间CMnew.Presenters { 公共类EditContactPresenter:PresenterBase { 私有只读ApplicationPresenter\u ApplicationPresenter; 私人联络(私人联络);; 公共EditContactPresenter(ApplicationPresenter应用程序Presenter,EditContactView视图,联系人):基本(视图,“Contact.LookupName”) { _applicationPresenter=applicationPresenter; _接触=接触; } 公众接触 { 获取{return\u contact;} 设置{u contact=value;} } public void SelectImage() { 字符串imagePath=View.AskUserForImagePath(); 如果(!string.IsNullOrEmpty(imagePath)) { Contact.ImagePath=ImagePath; } } 公共作废保存() { _applicationPresenter.SaveContact(联系人); } 公共作废删除() { _applicationPresenter.CloseTab(此); _applicationPresenter.DeleteContact(联系人); } 公众假期结束() { _applicationPresenter.CloseTab(此); } 公共覆盖布尔等于(对象对象对象) { EditContactPresenter presenter=对象作为EditContactPresenter; 返回presenter!=null&&presenter.Contact.Equals(Contact); } } } 名称空间CMnew.Views { /// ///EditContactView.xaml的交互逻辑 /// 公共部分类EditContactView:UserControl { 公共EditContactView() { 初始化组件(); } 公共编辑联系人演示者演示者 { 获取{作为EditContactPresenter返回DataContext;} } 私有无效保存\单击(对象发送者,路由目标) { Presenter.Save(); } 私有无效删除\单击(对象发送者,路由目标) { Delete(); } 私有无效关闭\单击(对象发送者,路由目标) { Presenter.Close(); } 私有void SelectImage\u单击(对象发送方,路由目标) { Presenter.SelectImage(); } 公共字符串AskUserForImagePath() { OpenFileDialog dlg=新建OpenFileDialog(); dlg.ShowDialog(); 返回dlg.FileName; } } },c#,sql-server,wpf,entity-framework,data-binding,C#,Sql Server,Wpf,Entity Framework,Data Binding,这可能是您的问题: public class Address : EntityBase { [ForeignKey("Contact")] // <--- public int AddressId { get; set; } public cl public int Add(Contact entity) { Context.Contacts.Add(entity); var o = SaveCha

这可能是您的问题:

public class Address : EntityBase
{
    [ForeignKey("Contact")] // <---
    public int AddressId { get; set; }  
public cl
    public int Add(Contact entity)
    {
        Context.Contacts.Add(entity);

        var o = SaveChanges();
        Context.Dispose();
        Context = new CMEntities();
        return o;
    }

    internal int SaveChanges()
    {
        try
        {
            return Context.SaveChanges();
        }
    }
namespace CMnew.Model
{
    public partial class ContactRepository : IDisposable
    {

        public CMEntities Context { get; set; } = new CMEntities();

        private List<Contact> _contactStore;

        public ContactRepository()
        {


            if (this.GetAll() == null)
            {
                _contactStore = new List<Contact>();
            }
            else
            {
                _contactStore = this.GetAll();
            }
        }


        public List<Contact> FindByLookup(string lookupName)
        {
            IEnumerable<Contact> found = from c in _contactStore
                                         where c.LookupName.StartsWith(lookupName, StringComparison.OrdinalIgnoreCase)
                                         select c;

            return found.ToList();
        }


        public List<Contact> FindAll()
        {
            return new List<Contact>(_contactStore);
        }


        public void Save(Contact contact)
        {
          
            if (_contactStore.Contains(contact))
            {
                this.SaveToDatabase(contact);
            }
            else
            {
                _contactStore.Add(contact);
                this.Add(contact);
            }
        }

        public int SaveToDatabase(Contact entity)
        {
           
            return SaveChanges();
        }

        public void Delete(Contact contact)
        {
            _contactStore.Remove(contact);
            DeleteFromDatabase(contact);
        }


        public int DeleteFromDatabase(Contact entity)
        {
            Context.Entry(entity).State = EntityState.Deleted;
            return SaveChanges();
        }

        public Contact GetOne(int? id) => Context.Contacts.Find(id);
        public List<Contact> GetAll() => Context.Contacts.ToList();

        public int Add(Contact entity)
        {

            Context.Contacts.Add(entity);

            var o = SaveChanges();

            Context.Dispose();
            Context = new CMEntities();

            return o;

        }

        internal int SaveChanges()
        {
            try
            {
                return Context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {

                throw;
            }

            catch (DbUpdateException ex)
            {
                throw;
            }

            catch (CommitFailedException ex)
            {
                throw;
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }


        bool disposed = false;
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                Context.Dispose();
            }

            disposed = true;
        }


    }
}


namespace CMnew.Presenters
{
    public class ApplicationPresenter : PresenterBase<Shell>, INotifyPropertyChanged
    {

        private readonly ContactRepository _contactRepository;
        private ObservableCollection<Contact> _currentContacts;

        public event PropertyChangedEventHandler PropertyChanged;

        public ApplicationPresenter(Shell view, ContactRepository contactRepository) : base(view)
        {
            _contactRepository = contactRepository;
            _currentContacts = new ObservableCollection<Contact>(_contactRepository.FindAll());
        }


        // public ObservableCollection<Contact> CurrentContacts { get; set; }

        public ObservableCollection<Contact> CurrentContacts
        {
            get { return _currentContacts; }
            set
            {
                _currentContacts = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentContacts)));

            }
        }


        public string StatusText { get; set; }

        public void Search(string criteria)
        {
            if (!string.IsNullOrEmpty(criteria) && criteria.Length > 2)
            {
                CurrentContacts = new ObservableCollection<Contact>(_contactRepository.FindByLookup(criteria));

                StatusText = string.Format("{0} contacts found.", CurrentContacts.Count);
            }
            else
            {
                CurrentContacts = new ObservableCollection<Contact>(_contactRepository.FindAll());
            }

        }


        public void NewContact()
        {
            OpenContact(new Contact());
        }


        public void SaveContact(Contact contact)
        {
            if (!CurrentContacts.Contains(contact))
            {
                CurrentContacts.Add(contact);
            }

            _contactRepository.Save(contact);

            StatusText = string.Format("Contact '{0}' was saved.", contact.LookupName);
        }

        public void DeleteContact(Contact contact)
        {
            if (CurrentContacts.Contains(contact))
            {
                CurrentContacts.Remove(contact);
            }

            _contactRepository.Delete(contact);

            StatusText = string.Format("Contact '{0}' was deleted.", contact.LookupName);
        }

        public void CloseTab<T>(PresenterBase<T> presenter)
        {
            View.RemoveTab(presenter);
        }



        private void OpenContact(Contact contact)
        {
            if (contact == null) return;

            View.AddTab(new EditContactPresenter(this, new EditContactView(), contact));
        }


        public void DisplayAllContacts()
        {
            throw new NotImplementedException();
        }

    }
}

namespace CMnew.Presenters
{
    public class EditContactPresenter : PresenterBase<EditContactView>
    {

        private readonly ApplicationPresenter _applicationPresenter;
        private Contact _contact;

        public EditContactPresenter(ApplicationPresenter applicationPresenter, EditContactView view, Contact contact) : base(view, "Contact.LookupName")
        {
            _applicationPresenter = applicationPresenter;
            _contact = contact;
        }

        public Contact Contact
        {
            get { return _contact; }
            set { _contact = value; }
        }

        public void SelectImage()
        {
            string imagePath = View.AskUserForImagePath();
            if (!string.IsNullOrEmpty(imagePath))
            {
                Contact.ImagePath = imagePath;
            }
        }

        public void Save()
        {
            _applicationPresenter.SaveContact(Contact);
        }

        public void Delete()
        {
            _applicationPresenter.CloseTab(this);
            _applicationPresenter.DeleteContact(Contact);
        }

        public void Close()
        {
            _applicationPresenter.CloseTab(this);
        }

        public override bool Equals(object obj)
        {
            EditContactPresenter presenter = obj as EditContactPresenter;
            return presenter != null && presenter.Contact.Equals(Contact);
        }

    }
}


namespace CMnew.Views
{
    /// <summary>
    /// Interaction logic for EditContactView.xaml
    /// </summary>
 
        public partial class EditContactView : UserControl
        {
            public EditContactView()
            {
                InitializeComponent();
            }

            public EditContactPresenter Presenter
            {
                get { return DataContext as EditContactPresenter; }
            }

            private void Save_Click(object sender, RoutedEventArgs e)
            {
                Presenter.Save();
            }

            private void Delete_Click(object sender, RoutedEventArgs e)
            {
                Presenter.Delete();
            }

            private void Close_Click(object sender, RoutedEventArgs e)
            {
                Presenter.Close();
            }

            private void SelectImage_Click(object sender, RoutedEventArgs e)
            {
                Presenter.SelectImage();
            }

            public string AskUserForImagePath()
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.ShowDialog();
                return dlg.FileName;
            }
        }
}

<UserControl x:Class="CMnew.Views.EditContactView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CMnew.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <DockPanel Margin="5">
        <Border DockPanel.Dock="Top">
            <DockPanel LastChildFill="False">
                <TextBlock DockPanel.Dock="Left" Text="{Binding Contact.LastName}"/>
                <TextBlock DockPanel.Dock="Left" Text=", "/>
                <TextBlock DockPanel.Dock="Left" Text="{Binding Contact.FirstName}"/>
                <TextBlock DockPanel.Dock="Right" Text="{Binding Contact.Organization}"/>
            </DockPanel>
        </Border>

        <StackPanel DockPanel.Dock="Bottom" Style="{StaticResource buttonPanel}">
            <Button Content="Save" Click="Save_Click"/>
            <Button Content="Delete" Click="Delete_Click"/>
            <Button Content="Close" Click="Close_Click"/>
        </StackPanel>

        <WrapPanel>
            <GroupBox BorderBrush="{StaticResource lightBlueBrush}">
                <GroupBox.Header>
                    <Border Background="{StaticResource lightBlueBrush}" Style="{StaticResource groupBoxHeader}">
                        <TextBlock Text="General"/>
                    </Border>
                </GroupBox.Header>

                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="175"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>


                    <Grid Grid.RowSpan="4">
                        <Border Background="Gray"
                                    CornerRadius="6"
                                    Margin="2 2 0 0"
                                    Opacity=".5"/>
                        <Border Margin="2 2 4 4"
                                    Background="White"/>
                        <Viewbox Margin="2 2 4 4">
                            <Image Source="{Binding Contact.ImagePath}" />
                        </Viewbox>
                        <Border BorderBrush="{StaticResource lightBlueBrush}"
                                    BorderThickness="2"
                                    Background="Transparent"
                                    CornerRadius="6"
                                    Margin="0 0 2 2"/>
                        <Button Style="{StaticResource openButton}"
                                    Background="White"
                                    Foreground="{StaticResource lightBlueBrush}"
                                    BorderBrush="{StaticResource lightBlueBrush}"
                                    ToolTip="Change Picture"
                                    Click="SelectImage_Click" />

                    </Grid>

                    <Label Grid.Column="1"
                               Content="_First Name:"
                               Target="{Binding ElementName=firstName}"/>
                    <TextBox x:Name="firstName"
                                 Grid.Column="2"
                                 Text="{Binding Contact.FirstName}"/>

                    <Label Grid.Row="1"
                               Grid.Column="1"
                               Content="_Last Name:"
                               Target="{Binding ElementName=lastName}"/>
                    <TextBox x:Name="lastName"
                                 Grid.Row="1"
                                 Grid.Column="2"
                                 Text="{Binding Contact.LastName}"/>

                    <Label Grid.Row="2"
                               Grid.Column="1"
                               Content="Or_ganization:"
                               Target="{Binding ElementName=organization}"/>
                    <TextBox x:Name="organization"
                                 Grid.Row="2"
                                 Grid.Column="2"
                                 Text="{Binding Contact.Organization}"/>


                    <Label Grid.Row="3"
                               Grid.Column="1"
                               Content="_Job Title:"
                               Target="{Binding ElementName=jobTitle}"/>
                    <TextBox x:Name="jobTitle"
                                 Grid.Row="3"
                                 Grid.Column="2"
                                 Text="{Binding Contact.JobTitle}"/>





                </Grid>

            </GroupBox>

            <GroupBox BorderBrush="{StaticResource greenBrush}">
                <GroupBox.Header>
                    <Border Background="{StaticResource greenBrush}"
                            Style="{StaticResource groupBoxHeader}">
                        <TextBlock Text="Address"/>
                    </Border>
                </GroupBox.Header>


                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="150"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="150"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Label Content="Line _1:"
                           Target="{Binding ElementName=line1}" />
                    <TextBox x:Name="line1"
                             Grid.Column="1"
                             Grid.ColumnSpan="3"
                             Text="{Binding Contact.Address.Line1}" />


                    <Label Grid.Row="1"
                           Content="Line _2:"
                           Target="{Binding ElementName=line2}" />
                    <TextBox x:Name="line2"
                             Grid.Row="1"
                             Grid.Column="1"
                             Grid.ColumnSpan="3"
                             Text="{Binding Contact.Address.Line2}" />



                    <Label Grid.Row="2"
                           Content="Ci_ty:"
                           Target="{Binding ElementName=city}" />
                    <TextBox x:Name="city"
                             Grid.Row="2"
                             Grid.Column="1"
                             Text="{Binding Contact.Address.City}" />


                    <Label Grid.Row="2"
                           Grid.Column="2"
                           Content="_State:"
                           Target="{Binding ElementName=state}" />
                    <TextBox x:Name="state"
                             Grid.Row="2"
                             Grid.Column="3"
                             Text="{Binding Contact.Address.State}" />


                    <Label Grid.Row="3"
                           Grid.Column="0"
                           Content="_Zip:"
                           Target="{Binding ElementName=zip}" />
                    <TextBox x:Name="zip"
                             Grid.Row="3"
                             Grid.Column="1"
                             Text="{Binding Contact.Address.Zip}" />



                    <Label Grid.Row="3"
                           Grid.Column="2"
                           Content="Countr_y:"
                           Target="{Binding ElementName=country}" />
                    <TextBox x:Name="country"
                             Grid.Row="3"
                             Grid.Column="3"
                             Text="{Binding Contact.Address.Country}" />


                </Grid>





            </GroupBox>

            <GroupBox BorderBrush="{StaticResource redBrush}">
                <GroupBox.Header>
                    <Border Background="{StaticResource redBrush}"
                            Style="{StaticResource groupBoxHeader}">
                        <TextBlock Text="Phone"/>
                    </Border>
                </GroupBox.Header>

                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="150"/>
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>


                    <Label Content="_Office:"
                           Target="{Binding ElementName=office}"/>
                    <TextBox x:Name="office"
                             Grid.Column="1"
                             Text="{Binding Contact.OfficePhone}" />


                    <Label Grid.Row="1"
                           Content="_Cell:"
                           Target="{Binding ElementName=cell}" />
                    <TextBox x:Name="cell"
                             Grid.Row="1"
                             Grid.Column="1"
                             Text="{Binding Contact.CellPhone}" />

                    <Label Grid.Row="2"
                           Content="_Home:"
                           Target="{Binding ElementName=home}" />
                    <TextBox x:Name="home"
                             Grid.Row="2"
                             Grid.Column="1"
                             Text="{Binding Contact.HomePhone}" />

                </Grid>

            </GroupBox>


            <GroupBox BorderBrush="{StaticResource brownBrush}">
                <GroupBox.Header>
                    <Border Background="{StaticResource brownBrush}"
                            Style="{StaticResource groupBoxHeader}">
                        <TextBlock Text="Email"/>
                    </Border>
                </GroupBox.Header>


                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="200"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>


                    <Label Content="_Primary:"
                   Target="{Binding ElementName=primaryEmail}"/>
                    <TextBox x:Name="primaryEmail"
                     Grid.Column="1"
                     Text="{Binding Contact.PrimaryEmail}"/>


                    <Label Grid.Row="1"
                        Content="S_econdary:"
                        Target="{Binding ElementName=secondaryEmail}"/>
                    <TextBox x:Name="secondaryEmail"
                     Grid.Row="1"
                     Grid.Column="1"
                     Text="{Binding Contact.SecondaryEmail}"/>

                </Grid>
            </GroupBox>
        </WrapPanel>

    </DockPanel>
</UserControl>

public class Address : EntityBase
{
    [ForeignKey("Contact")] // <---
    public int AddressId { get; set; }  
public ContactRepository()
        {


            if (this.GetAll() == null)
            {
                _contactStore = new List<Contact>();
            }
            else
            {
                _contactStore = this.GetAll();
            }
        }
public List<Contact> GetAll() => Context.Contacts.ToList();
private List<Contact> _contactStore = new List<Contact>();

        public ContactRepository()
        {

            _contactStore = this.GetAll();

            Context.Dispose();
            Context = new CMEntities();
     

        }