温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linq关系映射(1)

发布时间:2020-07-07 11:10:01 来源:网络 阅读:329 作者:Aonaufly 栏目:编程语言

linq关系映射(1)

C#的这种entity 很像Annotation java的写法..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace LinqObject.models
{
    [Table(Name = "Player")]
    public class PlayerInfoVO
    {
                         
        private string name;
        [Column(Name="name")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
                         
        private int id;
        [Column(IsPrimaryKey=true,Name="id")]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
                         
        private int level;
        [Column(Name = "level_id")]
        public int Level
        {
            get { return level; }
            set { level = value; }
        }
        private EntityRef<MyLevel> _myLevel;
        [Association(Storage="_myLevel",ThisKey="level_id")]
        public EntityRef<MyLevel> _MyLevel
        {
            get { return _myLevel; }
            set { _myLevel = value; }
        }
    }
}

主表:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace LinqObject.models{
    [Table(Name="myLevel")]
    public class MyLevel{
        private int id;
        [Column(IsPrimaryKey=true,Name="id")]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
        [Column(Name="name")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private EntitySet<PlayerInfoVO> player;
        [Association(Storage = "player", OtherKey = "Id")]
        public EntitySet<PlayerInfoVO> Player
        {
            get { return player; }
            set { player = value; }
        }
                     
    }
}

删除 操作 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinqObject.models;
namespace LinqObject.mylinq{
    public class ConflictLinq{
        public void updatePlayer(int id, string name) {
            AINYLinq db = new AINYLinq(@"Data Source=AONAUFLY\SQLEXPRESS;Initial Catalog=MyLINQ;User ID=sa;Password=******");
            IQueryable<MyLevel> myList = from o in db.myLevel
                         where o.Id == 0 && o.Name == "Aonaufly"
                         select o;
            foreach (var o in myList) {
                Console.WriteLine("Ainy------------------------");
                db.myLevel.DeleteOnSubmit(o);
            }
            Console.WriteLine("detail section finished.");
            Console.ReadLine();
            if (myList.Any())
            {
                Console.WriteLine("The parent is presesnt in the Orders collection.");
                try
                {
                    var ordF = (from o in db.myLevel
                                where o.Id == 0
                                select o).First();
                    db.myLevel.DeleteOnSubmit(ordF);
                    Console.WriteLine(ordF.Name);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                }
            }
            else {
                Console.WriteLine("There was no parent in the myLevel");
            }
            try
            {
                db.SubmitChanges();
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI