I am new to the MVVM pattern and WCF Data Services so please bear with me.
I am trying to create a solution with a WCF DataServices project (I have followed
https://msdn.microsoft.com/en-us/library/dd728275(v=vs.110).aspx for the DataService as a guide).
I then want to consume the service via a WPF application (I have followed
https://msdn.microsoft.com/en-us/library/dd728278(v=vs.110).aspx as a guide which works fine but I want to start using the MVVM pattern as this application is likely to get quite large so I want to start off "doing the right thing".
My problem is when trying to move the client logic into a class to attempt to start a MVVM model I am getting a very strange error:
Multiple types were found with the same name 'AdminUI.AdminServiceReference.tbl_Song, AdminUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Type names must be unique. C:\Users\USER\Documents\Visual Studio 2013\Projects\Admin\AdminUI\UCSongs.xaml targeting the following code:
<Grid.DataContext>
<local:SongsViewModel/>
</Grid.DataContext>
I have searched and searched online and I cannot work out why I am getting this error so I am hoping someone here may be able to put me out of my misery before I resort to logic within the UI.
I am not sure if this will be a factor in any answers but I also want the client to consume the WCF service in an asynchronous way eventually.
I hope providing the following code helps:
WCF Data Service using EntityFramework (MSSQL)
public class AdminWcfDataService : EntityFrameworkDataService<DBEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
}
}
Client ViewModel Class
public class SongsViewModel
{
DataServiceCollection<AdminUI.AdminServiceReference.tbl_Song> _songs = new DataServiceCollection<tbl_Song>();
public DataServiceCollection<AdminUI.AdminServiceReference.tbl_Song> Songs
{
get { return _songs; }
set { _songs = value; }
}
public SongsViewModel()
{
AdminUI.AdminServiceReference.DBEntities context = new AdminServiceReference.DBEntities(new Uri("http://localhost:59742/AdminWcfDataService.svc/"));
var listQuery = from mylist in context.tbl_Song
select mylist;
_songs = new System.Data.Services.Client.DataServiceCollection<AdminUI.AdminServiceReference.tbl_Song>(listQuery, System.Data.Services.Client.TrackingMode.AutoChangeTracking, "Songs", OnPropertyChanged, OnCollectionChanged);
}
private bool OnPropertyChanged(EntityChangedParams arg)
{
Console.WriteLine("Test property changed");
return false;
}
private bool OnCollectionChanged(EntityCollectionChangedParams arg)
{
Console.WriteLine("Test collection changed");
return false;
}
}
Client UI XAML
<UserControl x:Class="AdminUI.UCSongs"
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:AdminUI"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="grdMain">
<Grid.DataContext>
<local:SongsViewModel/>
</Grid.DataContext>
<ListView Name="lvMain" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding SongName}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
I am grateful for any answers / tips. If you require any extra information please let me know and I will try to update the question.