Sample Console Application

SQL Server Script


USE [PersistTest]
GO
/****** Object:  Table [dbo].[tblPage]    Script Date: 09/28/2008 00:34:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblPage](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[TestId] [int] NULL
) ON [PRIMARY]


USE [PersistTest]
GO
/****** Object:  Table [dbo].[tblTest]    Script Date: 09/28/2008 00:35:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblTest](
	[Name] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Age] [int] NULL,
	[Id] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]

Configuration

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<connectionStrings>
		<add name="SQLServer" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=PersistTest;Integrated Security=True; MultipleActiveResultSets=True"/>
	</connectionStrings>
</configuration>

Code


using System;
using System.Collections.Generic;
using System.Text;
using PersistentManager.Mapping;
using PersistentManager;
using System.Configuration;
using System.Collections;

namespace PersistentTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ConfigurationFactory factory = ConfigurationFactory.GetInstance(ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString);
            factory.ProviderDialect = ProviderDialect.SqlProvider;

            EntityManager manager = new EntityManager(factory);
            manager.OpenDatabaseSession();

            Test test = new Test();
            test.Name = "Ahmed";
            test.Age = 78;

            Page page = new Page();
            page.Name = "Test Page";
            test.Pages.Add(page);

            object id = manager.CreateNewEntity<Test>(ref test);

            Page page1 = new Page();
            page1.Name = "Name";

            Test test1 = new Test();
            test1.Age = 99;
            test1.Name = "Test 1";
            page1.Test = test1;

            
            manager.CreateNewEntity<Page>(ref page1);

            manager.ComitAndClose();
            factory.DestroyCurrentConnection();
        }
    }

    [Entity("tblPage")]
    public class Page
    {
        private int id;        

        [Key("Id", AutoKey = true)]
        public virtual int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string name;
        [Field("Name", false, true)]
        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }

        private Test test;
        [ManyToOne(typeof(Test), RelationColumn = "TestId", Cascade = Cascade.CREATE)]
        public Test Test
        {
            get { return test; }
            set { test = value; }
        }
    }

    [Entity("tblTest")]
    public class Test
    {
        private string name;
        private int age;
        private int id;

        [Key("Id", AutoKey = true)]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [Field("Name", false, true)]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [Field("Age", false, true)]
        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        private IList<Page> pages = new List<Page>();

        [OneToMany(typeof(Page), RelationColumn = "TestId", Cascade = Cascade.ALL)]
        public virtual IList<Page> Pages
        {
            get { return pages; }
            set { pages = value; }
        }
    }
}


Last edited Sep 28, 2008 at 8:35 AM by ahmedsalako, version 1

Comments

No comments yet.