无法使用EF + WCF在数据网格中添加行

时间:2016-04-08 13:34:47

标签: wpf entity-framework wcf datagrid ado.net

当使用Entity Framework访问数据库并对其进行修改时,我已经做好了一切。但是当尝试使用EF + WCF修改(添加记录)到DB时,我可以在Datagrid中检索表内容,但是我无法添加行,我相信我有不正确的GetAll方法实现:

public List<blog> GetAll()
{
    return db.blogs.ToList();
}

IService1.cs文件如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFSRV
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        int AddBlog(blog b);

        [OperationContract]
        List<blog> GetAll();

        ///[OperationContract]
        ///  void DeleteBlog(BlogService b);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WCFSRV.ContractType".
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

以下是Window Initialization上方法的调用:

private void Window_Initialized(object sender, EventArgs e)
{            
        DG1.ItemsSource = scli.GetAll();
}

这就是我得到的结果:

enter image description here

所以问题是如何让这个Datagrid解析行添加? (肯定属性Canuseraddrows设置为true)

感谢。

添加了xaml:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DataBaseP" mc:Ignorable="d" x:Class="DataBaseP.MainWindow"
    Title="MainWindow" Height="400" Width="700" Loaded="Window_Loaded" Initialized="Window_Initialized">
<Window.Resources>
    <CollectionViewSource x:Key="blogViewSource" d:DesignSource="{d:DesignInstance {x:Type local:blog}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource blogViewSource}">

    <DataGrid x:Name="DG1" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="10,10,208,96" ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="blogIDColumn" Width="SizeToHeader" Header="Blog ID" Binding="{Binding BlogID}" IsReadOnly="True"/>
            <DataGridTextColumn x:Name="authorColumn" Width="SizeToCells" Header="Author" Binding="{Binding Author}"/>
            <DataGridTextColumn x:Name="bsubjectColumn" Width="SizeToCells" Header="bsubject" Binding="{Binding bsubject}"/>
            <DataGridTextColumn x:Name="tagColumn" Width="SizeToCells" Header="Tag" Binding="{Binding Tag}"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button x:Name="AddBtn" Content="ADD/MODIFY" HorizontalAlignment="Left" Margin="510,56,0,0" VerticalAlignment="Top" Width="129" Height="44" Click="AddBtn_Click"/>

</Grid>

0 个答案:

没有答案