?
Solved

How create this view in sql server?

Posted on 2015-01-14
4
Medium Priority
?
72 Views
Last Modified: 2015-01-16
I need to create an view in sql server. I do this easily but this one is a little bit more complicated than I can do.
I have two tables.  

   [Table("PERSONS")]
    public partial class PERSON
    {
        public PERSON()
        {
            Movs = new HashSet<Mov>();
            Movs1 = new HashSet<Mov>();
        }

        public int PersonID { get; set; }

        [Required]
        [StringLength(50)]
        public string PersonName { get; set; }

        public virtual ICollection<Mov> Movs { get; set; }

        public virtual ICollection<Mov> Movs1 { get; set; }
    }

    public partial class Mov
    {
        public int MovID { get; set; }

        public int PersonID { get; set; }

        public int OtherPersonID { get; set; }

        public virtual PERSON PERSON { get; set; }

        public virtual PERSON PERSON1 { get; set; }
    }

and the context is:

 public partial class Model1 : DbContext
    {
        public Model1()
            : base("name=Model1")
        {
        }

        public virtual DbSet<Mov> Movs { get; set; }
        public virtual DbSet<PERSON> PERSONS { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PERSON>()
                .Property(e => e.PersonName)
                .IsUnicode(false);

            modelBuilder.Entity<PERSON>()
                .HasMany(e => e.Movs)
                .WithRequired(e => e.PERSON)
                .HasForeignKey(e => e.PersonID)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<PERSON>()
                .HasMany(e => e.Movs1)
                .WithRequired(e => e.PERSON1)
                .HasForeignKey(e => e.OtherPersonID)
                .WillCascadeOnDelete(false);
        }
    }



i need a view with both names.

how could I do that?
0
Comment
Question by:Deraldo Silva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Ray
ID: 40550194
If you get no other response, you should really consider adding this to one of the programming topics.  Most people in this topic (SQL) don't tend to speak sql in terms of developer coding (a few do though).
0
 

Author Comment

by:Deraldo Silva
ID: 40550222
Ok.
i have this relation in the db.
i need a view with both names.

thx for your attention
relation.png
0
 
LVL 9

Accepted Solution

by:
Valliappan AN earned 2000 total points
ID: 40554067
CREATE VIEW MovsView
AS
SELECT MovID, P.PersonName, OP.PersonName AS OtherPersonName
FROM Movs M
JOIN Persons P ON M.PersonID = P.PersonID
JOIN Persons OP ON M.OtherPersonID = OP.PersonID

Note: You may use left join, if personids can be NULL in Movs table.

HTH.
0
 

Author Closing Comment

by:Deraldo Silva
ID: 40554124
Thx
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question