I created a new test app
in the edmx i created two entities "Customer" and "Language" adding an association between the two so Customer has a language and Language has Customers
then I generated the sql to create the database
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 09/17/2010 19:39:39
-- Generated from EDMX file: c:\users\koen.skn\documents\visual studio 2010\Projects\WindowsFormsApplication1\ClassLibrary1\Model1.edmx
-- --------------------------------------------------
SET QUOTED_IDENTIFIER OFF;
GO
USE [test];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO
-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------
IF OBJECT_ID(N'[dbo].[FK_LanguageCustomer]', 'F') IS NOT NULL
ALTER TABLE [dbo].[Customers] DROP CONSTRAINT [FK_LanguageCustomer];
GO
-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------
IF OBJECT_ID(N'[dbo].[Customers]', 'U') IS NOT NULL
DROP TABLE [dbo].[Customers];
GO
IF OBJECT_ID(N'[dbo].[Languages]', 'U') IS NOT NULL
DROP TABLE [dbo].[Languages];
GO
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
-- Creating table 'Customers'
CREATE TABLE [dbo].[Customers] (
[Id] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(max) NOT NULL,
[Language_Id] int NOT NULL
);
GO
-- Creating table 'Languages'
CREATE TABLE [dbo].[Languages] (
[Id] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(max) NOT NULL
);
GO
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
-- Creating primary key on [Id] in table 'Customers'
ALTER TABLE [dbo].[Customers]
ADD CONSTRAINT [PK_Customers]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
-- Creating primary key on [Id] in table 'Languages'
ALTER TABLE [dbo].[Languages]
ADD CONSTRAINT [PK_Languages]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
-- Creating foreign key on [Language_Id] in table 'Customers'
ALTER TABLE [dbo].[Customers]
ADD CONSTRAINT [FK_LanguageCustomer]
FOREIGN KEY ([Language_Id])
REFERENCES [dbo].[Languages]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_LanguageCustomer'
CREATE INDEX [IX_FK_LanguageCustomer]
ON [dbo].[Customers]
([Language_Id]);
GO
-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
In my app I do this
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ClassLibrary1.Model1Container manager =
ClassLibrary1.Model1Container.DefaultManager;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ClassLibrary1.Language lang = new ClassLibrary1.Language();
lang.Name = "Dutch";
lang.EntityAspect.AddToManager();
ClassLibrary1.Customer customer = new ClassLibrary1.Customer();
customer.Name = "Koen";
customer.Language = lang;
customer.EntityAspect.AddToManager();
manager.SaveChanges();
}
private void button2_Click(object sender, EventArgs e)
{
foreach (var item in manager.Customers)
{
MessageBox.Show(item.Language.Name);
}
}
}
}
I click button 1 and then button 2 and i get a messagebox showing my language name
i close the app and then the debugger stops at the Langiage getter in the Customer:
#region Language property
/// <summary>Gets or sets the Language. </summary>
[Bindable(false)]
[Display(Name="Language", AutoGenerateField=false)]
[DataMember]
[IbEm.RelationProperty("LanguageCustomer", IbEm.QueryDirection.ToRole2)]
public Language Language {
get { return PropertyMetadata.Language.GetValue(this); }
set { PropertyMetadata.Language.SetValue(this, value); }
}
#endregion Language property
#endregion Navigation properties
giving this error : Index was outside the bounds of the array.
Can someone please help us out here, thx