Solved

How create this view in sql server?

Posted on 2015-01-14
4
63 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
  • 2
4 Comments
 
LVL 10

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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now